The difference and connection between "iOS knowledge learning" _int, Nsinteger, Nsuinteger, NSNumber

Source: Internet
Author: User
Tags numeric value

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.








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.