C primitive types

The collections covered in this chapter only hold objects. What if you want a collection of floats or ints? You can wrap common C number types using NSNumber.

You can create a literal NSNumber instance using the @ symbol – similar to how you create literal NSString instances. For instance, if you wanted to put the numbers 4 and 5.6 into an array, you would create the instance of NSNumber and then add the NSNumber object to the array:

N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​l​i​s​t​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
[​l​i​s​t​ ​a​d​d​O​b​j​e​c​t​:​@​4​]​;​
[​l​i​s​t​ ​a​d​d​O​b​j​e​c​t​:​@​5​.​6​]​;​

Note that you cannot do math directly with an NSNumber, only with primitives. You must ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.