How get the total sum of NSNumber's from a NSArray?

neowinston picture neowinston · Jun 25, 2012 · Viewed 19.8k times · Source

I have a large NSArray containing NSNumbers like 3, 4, 20, 10, 1, 100, etc...

How do I get the total sum of all these NSNumbers (3 + 4 + 20 + 10 + 1 + 100 + etc...) as one total NSInteger?

Thank you!

Answer

Paul de Lange picture Paul de Lange · Jun 25, 2012

You can use this:

NSArray* numbers = //array of numbers
NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"];