OBJECTIVE-C number types in programming (Nsinteger,nsuinteger,nsnumber)

Source: Internet
Author: User

In Objective-c, we can use the numeric data types in C, int, float, long, and so on. They are all basic data types, not objects. That is, it is not possible to send messages to them. Then, there are times when you need to use these values as objects.


Nsinteger, Nsuinteger


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. Nsinteger is recommended 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. Nsuinteger are unsigned, that is, there are no negative numbers, and Nsinteger are signed.


NSNumber


Some people say that since there are nsinteger and other basic types, why should there be nsnumber?
NSNumber can wrap a basic data type to form an object so that it can be sent a message and loaded into Nsarray medium.

Nsinteger intval = 123; NSNumber *numberval = [NSNumber numberwithinteger:intval];  nsmutablearray* array = [Nsmutablearray array];    [Array addobject:intval]; Error, Intval is not an object type [array Addobject:numberval]; That's right

Cocoa provides a basic data type that is packaged (that is, implemented as an object) by the NSNumber class.

+ (nsnumber*) Numberwithchar: (char) value;+ (nsnumber*) Numberwithint: (int) value;+ (nsnumber*) numberwithdouble: ( Double) value;+ (nsnumber*) Numberwithfloat: (float) 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;-(double) doublevalue;-(BOOL) boolvalue;

Nsvalue class


But we also tend to have such requirements, such as the need to store structures such as Cgpoint or CGRect in a collection. A Nsvalue object is a simple container for storing a C or objective-c data. It can save any type of data, such as Int,float,char, of course, it can refer to pointers, structures, and object IDs. The goal of the Nsvalue class is to allow data structures of the above types to be added to the collection. Is this structure transformed into an object, not a boxed (boxing) in Java? But in objective-c it's called packaging (wraping), on the contrary, The basic type, called unwind (unwraping), is extracted from the object and is called unboxing (unboxing) in Java.

NSNumber inherit from NSObject, you can use compare, isequal and other messages.
NSNumber is a subclass of Nsvalue. Nsvalue can wrap any type of value.

This article is from the "Green Peak" blog, please make sure to keep this source http://zhuxianzhong.blog.51cto.com/157061/1598644

OBJECTIVE-C number types in programming (Nsinteger,nsuinteger,nsnumber)

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.