Objective-c NSNumber method

Source: Internet
Author: User

The contents of these days are mostly methods, may be seen in the time feel there is nothing to learn, but these things will be involved in the follow-up, it is best to be able to knock again, so in the back of the time will not feel strange!

NSNumber is a class that inherits from Nsvalue that is a collection of basic data types including char a signed or unsigned char, Short int, int, long int, long long int, float, or double or as a BOOL        /c17> The NSNumber type is a bit like the ID type, which can be declared for any type of numeric object, that is, it is used to declare a numeric object, it is very difficult to determine what type of number the declaring variable is, and to determine that the Number object type is determined at initialization time.

1. When you need to use a variable of type int, you can use int or Nsinteger as a program of C, but we recommend using Nsinteger, because you don't have to consider whether the device is 32-bit or 64-bit.

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 value in Nsmutablearray, it is not possible to use Nsinteger directly , such as in a Nsmutablearray :

Nsmutablearray *array = [[Nsmutablearray alloc]init];

[Array addobject:[nsnumber numberwithint: []];

This will cause a compilation error, because the nsmutablearray inside the need is a class, but ' "" is not aclass.

Use the NSNumber class to wrap (that is, to implement as an object) the basic data type.

For example, the following creation method:

+ (NSNumber *) Numberwithchar: (char) value;//Convert char

+ (NSNumber *) Numberwithint: (int) value;//conversion int

+ (NSNumber *) Numberwithfloat: (float) value;//convert float

+ (NSNumber *) Numberwithbool: (BOOL) value;//Conversion BOOL

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;

Example:

NSNumber *num = [NSNumber numberwithint:];

nsinteger integer = [num intvalue];

the mutual conversion between NSString and Nsinteger

Nsinteger integernumber = 88;

NSString * string = [NSString stringWithFormat:@ "%d", integernumber];

NSLog (@ "string is%@", string);

Integer = [string intvalue];

NSLog (@ "integer is%d", integernumber);

Objective-c NSNumber method

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.