1, first understand the following NSNumber type:
Apple Official document Address: Https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_ Class/reference/reference.html
NSNumber is a subclass of Nsvalue, which is an object to store numeric values including bool type, which provides a series of methods to store char a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL, which provides a compare: method to determine the ordering of two NSNumber objects;
Creating a NSNumber object has the following methods:
+ numberwithbool:+ numberwithchar:+ numberwithdouble:+ numberwithfloat:+ numberwithint:+ numberWithInteger:+ numberwithlong:+ numberwithlonglong:+ numberwithshort:+ numberwithunsignedchar:+ numberWithUnsignedInt:+ numberwithunsignedinteger:+ numberwithunsignedlong:+ numberwithunsignedlonglong:+ NumberWithUnsignedShort:
Initialization method:
–initwithbool:–initwithchar:–initwithdouble:–initwithfloat:–initwithint:–initwithinteger:–initwithlong:– initwithlonglong:–initwithshort:–initwithunsignedchar:–initwithunsignedint:–initwithunsignedinteger:– Initwithunsignedlong:–initwithunsignedlonglong:–initwithunsignedshort:
Retrieval
–boolvalue–charvalue–decimalvalue–doublevalue–floatvalue–intvalue–integervalue–longlongvalue–longvalue– shortvalue–unsignedcharvalue–unsignedintegervalue–unsignedintvalue–unsignedlonglongvalue–unsignedlongvalue– Unsignedshortvalue
The NSNumber type is a bit like the ID type, which can be declared for any type of numeric object, that is, it is used to declare a numeric object, it is very difficult to determine what type of number the declaring variable is, and to determine that the Number object type is determined at initialization time.
Creation or initialization of a numeric object:
Format:
NSNumber Numeric Object = [NSNumber numberwith numeric type: numeric];
intnumber = [NSNumber numberwithint:100];longnumber = [NSNumber numberwithlong:0xabcdef];floatnumber = [NSNumber NUMBERWITHFLOAT:10.01];
2.
differences and linkages between int, Nsinteger, Nsuinteger, NSNumber
int: When using the int type to define a variable, you can use an int or Nsinteger as a C program, and we recommend using the Nsinteger, because you don't have to consider whether the device is 32-bit or 64-bit.
Nsuinteger are unsigned, that is, there are no negative numbers, and Nsinteger are signed.
Nsinteger is the underlying type, NSNumber is a class, if you need to store a numeric value, the direct use of nsinteger is not possible, such as in an array using the following statement will be an error:
Nsarray *array = [Nsarray alloc] init]; [Array Addobject:3];
Because the array should be a class, but ' 3 ' is not, so you need to use NSNumber:
Nsarray *array = [Nsarray alloc] init]; [Array Addobject:[nsnumber numberwithint:3];
It's easier to write and hopefully helpful.