If you’ve ever found yourself scratching your head thinking “now which one should I be using, NSNumber or NSInteger?” the short summary below should help.
NSInteger is nothing more than a synonym for a long integer. What follows is how NSInteger is defined:
1 2 3 4 5 6 7
#if __LP64__ || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif
NSNumber is an Objective-C class, a subclass of NSValue to be specific. You can create an NSNumber object from a signed or unsigned char, short int, int, long int, long long int, float, double or BOOL.
One of the primary distinctions is that you can use NSNumber in collections, such as NSArray, where an object is required. For example, if you need to add a float into an NSArray, you would first need to create an NSNumber object from the float:
1 2 3 4
float percentage = 40.5; ... // Create NSNumber object, which can now be inserted into an NSArray NSNumber *percentageObject = [NSNumber numberWithFloat:percentage];
NSInteger is a simply an integer, NSNumber is an object.