The use of iOS development int,nsinteger,nsuinteger,nsnumber

Source: Internet
Author: User
Tags bool instance method

 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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.