OC Basic Tutorial 10-nsnumber Details

Source: Internet
Author: User
Tags float double

NSNumber Introduction

NSNumber is the object form of numbers because only objects are allowed in the OC Array and dictionary, so we sometimes need to convert

Our normal type is 123 so the NSNumber type is @123, which is an object


1. Convert int float double to nsnumber type

Viewing the system's APIs we can see the following methods to generate the NSNumber type:

+ (NSNumber *) Numberwithchar: (char) value;+ (NSNumber *) Numberwithunsignedchar: (unsigned char) value;+ (NSNumber *) Numberwithshort: (short) value;+ (NSNumber *) Numberwithunsignedshort: (unsigned short) value;+ (NSNumber *) Numberwithint: (int) value;+ (NSNumber *) Numberwithunsignedint: (unsigned int) value;+ (NSNumber *) Numberwithlong: (Long ) value;+ (NSNumber *) Numberwithunsignedlong: (unsigned long) value;+ (NSNumber *) Numberwithlonglong: (Long Long) value;+ (NSNumber *) Numberwithunsignedlonglong: (unsigned long Long) value;+ (NSNumber *) Numberwithfloat: (float) value;+ ( NSNumber *) Numberwithdouble: (double) value;+ (NSNumber *) Numberwithbool: (BOOL) value;+ (NSNumber *) Numberwithinteger :(Nsinteger) value ns_available (10_5, 2_0); + (NSNumber *) Numberwithunsignedinteger: (Nsuinteger) value ns_available (10 _5, 2_0);

Examples of Use:

        1. Convert int float double to corresponding object class                int aa1 = +;        NSNumber *aa2 = @ (AA1);//Direct use @ symbol conversion        nsnumber *aa3 = [NSNumber numberwithint:aa1];//use method to convert        float bb1 = 13.4;        NSNumber *bb2 = [NSNumber numberwithfloat:bb1];        NSLog (@ "AA2 =%@,BB2 =%@", aa2,bb2);


2. Convert NSNumber to normal data type

We can get its internal values through the properties and methods of the NSNumber.

@property (readonly) char charvalue; @property (readonly) unsigned char unsignedcharvalue; @property (readonly) Short Shortvalue; @property (readonly) unsigned short unsignedshortvalue; @property (readonly) int intvalue; @property ( readonly) unsigned int unsignedintvalue; @property (readonly) long Longvalue; @property (readonly) unsigned long Unsignedlongvalue; @property (readonly) long long Longlongvalue; @property (readonly) unsigned long long Unsignedlonglongvalue; @property (readonly) float Floatvalue; @property (readonly) double doublevalue; @property ( readonly) BOOL boolvalue; @property (readonly) Nsinteger IntegerValue ns_available (10_5, 2_0); @property (readonly) Nsuinteger unsignedintegervalue ns_available (10_5, 2_0); @property (readonly, copy) NSString *stringvalue;

Examples of Use:

   2. Get the numeric value in NSNumber        nsnumber *cc1 = @123;        int CC2 = [cc1 intvalue];//gets the integer value                nsnumber *dd1 = @123.456;        Double DD2 = [dd1 doublevalue];//gets double value        double dd3= dd1.intvalue;//Property mode

Comparison between 3.NSNumber

-(Nscomparisonresult) Compare: (NSNumber *) othernumber;-(BOOL) Isequaltonumber: (NSNumber *) number;
4.NSNumber stored in an array or dictionary

        Nsarray *arr = @[@1,@123,@2];        NSNumber *num1 = arr[0];        NSNumber *num2 = [arr objectatindex:1];                Nsdictionary *dic = @{@ "Key1": @1,@ "Key2": @2};        NSNumber *nu1 = dic[@ "Key1"];        NSNumber *nu2 = [dic objectforkey:@ "Key2"];







OC Basic Tutorial 10-nsnumber Details

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.