The difference between nsinteger,nsnumber and int in C language

Source: Internet
Author: User
Tags instance method


Nsinteger and NSNumber

First of all: Nsinteger,nsnumber has nothing to do with it, not to assume that the two have any inheritance relationship, and even asked Nsinteger is not nsnumber subclass? The answer is NO!!! Nsinteger is only a basic data type, and NSNumber is the object of OC, and NSNumber inherits from Nsvalue,nsvalue and inherits from NSObject.

Since Nsinteger is the basic type, it is very simple to use. Declare a variable, nsinteger the myInt, and directly assign the value to it. For example: myint=8;


The class methods owned by NSNumber are as follows:


+ (nsnumber*) Numberwithchar: (char) value;

+ (nsnumber*) Numberwithint: (int) value;

+ (nsnumber*) Numberwithfloat: (float) value;

+ (nsnumber*) Numberwithbool: (BOOL) value;

NSNumber *mynumber,*floatnumber,*intnumber,*doublenumber,*charnumber;

MyNumber = [NSNumber numberwithlong:0xabcdef];

Floatnumber = [NSNumber numberwithfloat:12.34];

intnumber = [NSNumber numberwithint:1000];

Doublenumber = [NSNumber numberwithdouble:12345e+15];

CharNumber = [NSNumber numberwithchar: ' X '];

After the base type data is encapsulated into NSNumber, it can be retrieved by using the following instance method:

-(char) charvalue;

-(int) intvalue;

-(float) floatvalue;

-(BOOL) boolvalue;

-(nsstring*) stringvalue;

MyInt = [Intnumber intvalue]; Get an integer value of an object

[Floatnumber Floatvalue];

[Doublenumber Doublevalue];

[CharNumber Charvalue];

About Nsinteger and int:

In C, the number of bytes in int and long is equal to the number of digits in the operating system pointer.


But the C language says long is always greater than or equal to int

In Objective-c, Apple's official document always recommends Nsinteger.

What is the difference between it and int, StackOverflow This helps the great god to give the answer.

Nsinteger is an encapsulation that identifies the number of digits of the current operating system and automatically returns the largest type. When you don't know what your operating system is, you usually want to use Nsinteger, so maybe you want your int type range as large as possible, with nsinteger,32 bit system Nsinteger is an int, that's 32 bits, but then 64-bit system, Nsinteger is 64-bit. --so it's generally recommended with Nsinteger.

The code defined is similar to the following:

#if __lp64__ | | target_os_embedded | | Target_os_iphone | | Target_os_win32 | | Ns_build_32_like_64

typedef long Nsinteger;

typedef unsigned long Nsuinteger;

#else

typedef int Nsinteger;

typedef unsigned int nsuinteger;

#endif

You are usually want to use Nsinteger while you don ' t know what kind of processor architecture code your run Ay for some reason want the largest possible int type, which on bit systems are just an int, while on a 64-bit system it ' s a long.

Summary: The difference between Nsinteger and int is that Nsinteger automatically selects the maximum number of int (int or long) based on the number of digits (32OR64) of the system.

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.