Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between NSNumber and NSInteger?

What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats?

like image 492
mk12 Avatar asked Aug 16 '09 19:08

mk12


People also ask

What is NSInteger in objective C?

Example# The NSInteger is just a typedef for either an int or a long depending on the architecture. The same goes for a NSUInteger which is a typedef for the unsigned variants.

What is NSNumber in Swift?

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .


1 Answers

NSNumber is a class, not a primitive, and is used when you need to put raw numbers into dictionaries, arrays, or otherwise encapsulate them. NSInteger, NSUInteger, CGFloat, etc are simple types and correspond (on 32-bt systems like the iPhone) to int, unsigned int and float.

As a general rule, if you need to store a number somewhere, use NSNumber. If you're doing calculations, loops, etc, use NSInteger, NSUInteger or CGFloat.

You can wrap an NSInteger into an NSNumber:

NSNumber *aNumber = [NSNumber numberWithInteger:21]; 

... and get it back:

NSInteger anInteger = [aNumber integerValue]; 

You can find out more info here: http://iosdevelopertips.com/cocoa/nsnumber-and-nsinteger.html

like image 160
iKenndac Avatar answered Oct 05 '22 18:10

iKenndac