1. When int type variables are required, you can use int or NSInteger, just like the program that writes C, but NSInteger is recommended, in this case, you do not need to consider whether the device is 32-bit or 64-bit.
2. NSUInteger is unsigned, that is, there is no negative number, and NSInteger is signed.
3. Some people say that since NSInteger and other basic types exist, why does NSNumber exist? Of course, their functions are different.
NSInteger is a basic type, but NSNumber is a class. If you want to store a value, NSInteger cannot be used directly. For example, in an Array, NSInteger is used as follows:
NSArray * array = [[NSArray alloc] init];
[Array addObject: 3]; // compilation Error
This will cause compilation errors because NSArray requires a class, but '3' is not. NSNumber is required at this time:
NSMutableArray * array = [[NSMutableArray alloc] init];
[Array addObject: [NSNumber numberWithInt: 3];
There will be a warning for the two lines of code because NSArray is immutable.
NSArray * array1 = [[NSArray alloc] init];
[Array1 addObject: [NSNumber numberWithInt: 3];
Cocoa provides the NSNumber class for packaging (that is, implemented in the form of objects) Basic data types.
For example:
+ (NSNumber *) numberWithChar: (char) value;
+ (NSNumber *) numberWithInt: (int) value;
+ (NSNumber *) numberWithFloat: (float) value;
+ (NSNumber *) numberWithBool: (BOOL) value;
After encapsulating the basic data type into NSNumber, you can obtain it again using the following instance method:
-(Char) charValue;
-(Int) intValue;
-(Float) floatValue;
-(BOOL) boolValue;
-(NSString *) stringValue;