[The difference between]ios Nsinteger/nsuinteger and int/unsigned int, long/unsigned long

Source: Internet
Author: User

Nsinteger and Nsuinteger are often used in iOS development, and in other languages like C + +, we often use int, unsigned int. We know that iOS can also use the g++ compiler, so what's the connection between them?

Such statements are found in the definition files of Nsuinteger and Nsinteger NSObjCRuntime.h:

#if __lp64__ | | (target_os_embedded &&!) Target_os_iphone) | | Target_os_win32 | | Ns_build_32_like_64typedeflong nsinteger;typedefunsigned long Nsuinteger; #elsetypedefint nsinteger;typedefunsigned int Nsuinteger; #endif

The difference between Nsinteger and int,nsuinteger and unsigned int can be clearly seen here. The Mac OS X system is __lp64__, and the latter refers to the specific target hardware device. Therefore the Nsinteger/nsuiteger is not exactly equal to the corresponding int/unsigned int, nor is it exactly equal to the corresponding long/unsigned long. It depends on the specific operating environment and its hardware device architecture.

To better understand the definition above, you can refer to the following:

You usually want to use when do you NSInteger don ' t know what kind of processor architecture your code might run on, so you may F Or some reason want the largest possible int type, which on + bit systems are just an int and while on a 64-bit system it ' s a long .

When you don't know which processor architecture The program is running, you'd better use Nsinteger, because it's possible that int is just int on a 32-bit system, whereas in a 64-bit system, int can be a long type.

I ' d stick with using NSInteger instead of int / long unless you specifically require them.

Keep using Nsinteger unless you have to use the Int/long type.

As you can see from the definition above, Nsinteger/nsuinteger is a dynamically defined type, with different devices, different schemas, possibly an int type, and possibly a long type.

With regard to the correct format specifier your should use for each of the these types, see the String Programming Guide ' s sec tion on Platform Dependencies

For proper use of these types, refer to String Programming Guide's section on Platform Dependencies.

to make it easier to know the size of Nsinteger and long, we just need to remember that their size is always equal to the size of the pointer, that is 32bit in the 32bit system, and the 64bit system size is always 64bit. (and is always NSInteger long pointer-sized.) That's means they ' re 32-bits on 32-bit systems, and the bits on 64-bit systems.)

1. When you need to use a variable of type int, you can use int or Nsinteger as a program written C, but it is more recommended that Nsinteger be used, as this will not be considered 32-bit or 64-bit for the device.

2.NSUInteger is unsigned, that is, there are no negative numbers, and the Nsinteger is signed.

3. It is said that since all of these basic types are nsinteger, why should there be 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:
Nsarray *array = [[Nsarray alloc]init]; [Array 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: (Flo at) 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;

[The difference between]ios Nsinteger/nsuinteger and int/unsigned int, long/unsigned long

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.