"iOS Knowledge Learning" _int, Nsinteger, Nsuinteger, nsnumber differences and connections

Source: Internet
Author: User

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.








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.