1, when you need to use a variable of type int, you can do the same as a C program, with an int, or you can use a nsinteger, but it is more recommended to use Nsinteger, because you don't have to consider whether the device is 32-bit or 64-bit.
2, Nsuinteger is unsigned, that is, there are no negative numbers, Nsinteger is signed.
3, some people say that since there are nsinteger and so on these basic types why still have nsnumber? Their functions are of course different. The
Nsinteger is the underlying type, but NSNumber is a class. If you want to store a value, it is not possible to use Nsinteger directly, such as in an array:
Nsarray *array= [[Nsarray alloc]init];
[Array addobject:3];//compiles errors
This can cause compilation errors because the Nsarray inside is a class, but ' 3 ' is not. This time you need to use the NSNumber:
Nsmutablearray *array= [[Nsmutablearray alloc]init];
[Array Addobject:[nsnumber numberwithint:3]];
Two lines of code can be a warning because the Nsarray is immutable.
Nsarray *array1= [[Nsarray alloc]init];
[Array1 Addobject:[nsnumber Numberwithint:3]]; The
cocoa provides a nsnumber class to wrap (that is, implement as an object) the basic data type.
For example, the following creation method:
+ (nsnumber*) Numberwithchar: (char) value;
+ (nsnumber*) Numberwithint: (int) value;
+ ( nsnumber*) Numberwithfloat: (float) value;
+ (nsnumber*) Numberwithbool: (BOOL) value; After the
encapsulates the base type data into the NSNumber, it can be retrieved by the following instance method:
-(char) charvalue;
-(int) intvalue;
-(float) floatvalue;
-(BOOL) boolvalue;
-(nsstring*) stringvalue;