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:
[OBJC]View Plaincopy
- + Numberwithbool:
- + Numberwithchar:
- + numberwithdouble:
- + Numberwithfloat:
- + Numberwithint:
- + Numberwithinteger:
- + Numberwithlong:
- + Numberwithlonglong:
- + Numberwithshort:
- + Numberwithunsignedchar:
- + Numberwithunsignedint:
- + Numberwithunsignedinteger:
- + Numberwithunsignedlong:
- + Numberwithunsignedlonglong:
- + Numberwithunsignedshort:
Initialization method:
[OBJC]View Plaincopy
- – Initwithbool:
- – Initwithchar:
- – initwithdouble:
- – initwithfloat:
- – Initwithint:
- – Initwithinteger:
- – Initwithlong:
- – Initwithlonglong:
- – Initwithshort:
- – Initwithunsignedchar:
- – Initwithunsignedint:
- – Initwithunsignedinteger:
- – Initwithunsignedlong:
- – Initwithunsignedlonglong:
- – Initwithunsignedshort:
Retrieval
[OBJC]View Plaincopy
- –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];
[OBJC]View Plaincopy
- intnumber = [NSNumber numberwithint:100];
- Longnumber = [NSNumber numberwithlong:0xabcdef];
- Floatnumber = [NSNumber numberwithfloat:10. 01];
2,int, Nsinteger, Nsuinteger, nsnumber between the difference and contact
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.
1, when you need to use the int type of variable, you can write C as a program, with int, can also use Nsinteger, but it is more recommended to use Nsinteger, because this does not have to consider whether the device is 32-bit or 64-bit.
2 ", Nsuinteger is unsigned, that is, there is no negative, Nsinteger is signed.
3, some people say that since all have nsinteger and so on these basic types why still have nsnumber? Their functions are, of course, different.
Nsinteger is the underlying type, but NSNumber is a class. If you want to store a numeric value, it is not possible to use Nsinteger directly, such as in an array:
Nsarray *array= [[Nsarray alloc]init];
[Array addobject:3];//will compile error
This will cause a compilation error, because the need to put in Nsarray 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];
The two lines of code will have a warning because the Nsarray is immutable.
Nsarray *array1= [[Nsarray alloc]init];
[Array1 Addobject:[nsnumber Numberwithint:3];
Cocoa provides a basic data type that is packaged (that is, implemented as an object) by the NSNumber class.
For example, the following creation method:
+ (nsnumber*) Numberwithchar: (char) value;
+ (nsnumber*) Numberwithint: (int) value;
+ (nsnumber*) Numberwithfloat: (float) value;
+ (nsnumber*) Numberwithbool: (BOOL) value;
Once the base type data is encapsulated in NSNumber, it can be retrieved by using the following instance method:
-(char) charvalue;
-(int) intvalue;
-(float) floatvalue;
-(BOOL) boolvalue;
-(nsstring*) stringvalue;
Reference:
http://www.wuleilei.com/blog/335
http://blog.csdn.net/weasleyqi/article/details/33396809