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:

1 #if __lp64__ | | (target_os_embedded &&!) Target_os_iphone) | | Target_os_win32 | | ns_build_32_like_642long  Nsinteger; 3 Long Nsuinteger; 4 #else 5 int Nsinteger; 6 int Nsuinteger; 7 #endif

Therefore, the following code can be changed to:

int pvnums = [[[Nsuserdefaults Standarduserdefaults] objectforkey:@ "Pvlimit"] integervalue];

Modified to:

Nsinteger pvnums = [[[Nsuserdefaults Standarduserdefaults] objectforkey:@ "Pvlimit"] integervalue];


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 the Nsinteger when you don ' t know what kind of processor architecture your code might run on, so Y OU may for some reason want the largest possible int type, which on + bit systems is just an int, while on a 64-bit syste M 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 the using Nsinteger instead of Int/long unless you specifically require.

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. (Nsinteger and Long is always pointer-sized.) That's means they ' re 32-bits on 32-bit systems, and the bits on 64-bit systems.)

Summarize:

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. (inside a 32-bit system, Nsinterger is equivalent to int, Nsinterger is equivalent to long in a 64-bit system)

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:

1 nsarray *array = [Nsarray alloc] init]; 2 [array addobject:3];

Because the array should be a class, but ' 3 ' is not, so you need to use NSNumber:

1 nsarray *array = [Nsarray alloc] init]; 2 [array addobject:[nsnumber numberwithint:3]];

It's easier to write and hopefully helpful.

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.