1, first understand the next 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, an object that stores numeric values including bool, which provides a series of methods for storing 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 somewhat similar to the ID type, which can be declared for any type of numeric object, that is, it is used to declare a numeric object, and it is difficult to determine what numeric type the declaring variable is, and to determine how many types of numeric objects are initialized.
The creation or initialization of a numeric object:
Format:
NSNumber Numeric Object = [NSNumber numberwith numeric type: numeric value];
intnumber = [NSNumber numberwithint:100];
Longnumber = [NSNumber numberwithlong:0xabcdef];
Floatnumber = [NSNumber numberwithfloat:10.01];
2,
the differences and connections between int, Nsinteger, Nsuinteger, NSNumber
int: When you use the int type to define a variable, you can use the Nsinteger as a C program, using an int as well as a nsinteger, because you don't have to consider whether the device is 32 bits or 64 bits.
Nsuinteger is unsigned, that is, there are no negative numbers, Nsinteger are signed.
Nsinteger is the basic type, NSNumber is a class, if you need to store a number, 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]];
Write more simple, hope to be helpful.