Objective-C: Learning 09-NSNumber and smile syntax, objectivecnsnumber

Source: Internet
Author: User

Objective-C: Learning 09-NSNumber and smile syntax, objectivecnsnumber
The NSNumber array, dictionary, and set can only store data of the object type in these three containers. If you want to store data of the basic data type in these three containers, to convert the basic data type to the object type, you need to use the NSNumber class. NSNumber is inherited from NSValue and number class. It is mainly used to convert the basic data type to the NSNumber object type and convert the object type to the basic data type.

Integer

Int a = 100; create an NSnumber objectNumberWithInt:NSNumber * intNumber = [NSNumber numberWithInt: a]; converts the basic data type to NSLog (@ "intNumber = % @", intNumber). Otherwise, convert object type to basic data type. IntValueInt B = intNumber. intValue; // attribute, equivalent to [intNumber intvalue]; NSLog (@ "B = % d", B );

13:06:17. 252 OCNSNumber [1659: 136457] intNumber = 100

13:06:17. 253 OCNSNumber [1659: 136457] B = 100

 

Floating Point Type

    float c = 3.14;    NSNumber *floatNumber = [NSNumber numberWithFloat:c];  numberWithFloat:     NSLog(@"floatNumber = %@", floatNumber);    float d = floatNumber.floatValue;   floatValue    NSLog(@"d = %.2f", d);

Character Type

Char e = 'a'; Convert character type to numeric objectNumberWithChar:NSNumber * charNumber = [NSNumber numberWithChar: e]; NSLog (@ "charNumber = % @", charNumber );
Char f = charNumber. charValue;. CharValue
NSLog(@"f = %c", f);

Short integer

    short g = 10;    NSNumber *shortNumber = [NSNumber numberWithLong:g];   numberWithLong:    NSLog(@"shortNumber = %@", shortNumber);       short h = shortNumber.shortValue;   .shortValue    NSLog(@"h = %d", h);

 

Small exercises

// Int n1 = 10, n2 = 22, n3 = 18, n4 = 15; Requirement: Put the above four variables in the array object, then sort // convert to object type NSNumber * m1 = [NSNumber numberWithInt: n1]; NSNumber * m2 = [NSNumber numberWithInt: n2]; NSNumber * m3 = [NSNumber numberWithInt: n3]; NSNumber * m4 = [NSNumber numberWithInt: n4]; // in the array object, NSArray * array = [NSArray arrayWithObjects: m1, m2, m3, m4, nil]; // sort the elements in the array object. NSArray * sortArray = [array sortedArrayUsingSelector: @ selector (compare :)]; NSLog (@ "% @", sortArray );

13:15:23. 908 OCNSNumber [1667: 140688] (

10,

15,

18,

22

)

  Summary: Convert the basic data type to an object: use [NSNumber numberWith + type name of the basic data type]; otherwise, convert the object type to the basic data type: Basic Data Type + Value;

Smile syntax

The following is the use of some laughter syntaxes.

The smile syntax, that is, the literal volume, is a simple way to express data.

  1. The data representation of the object data type can be directly used @

For example, for example, for example, 100, the general practice is:

NSNumber * number = [NSNumber numberWithInt: 100];

NSLog (@ "% @", number );

You can print objects in this way using the smile syntax.

NSLog (@ "% @", @ 100 );

 

 2. The smile syntax of the immutable array uses @ [] to put objects in brackets

NSArray * nameArray2 = @ [@ "zhangSan", @ "liSi", @ "wangEr"];

NSLog (@ "% @", nameArray2 );

3. variable array smile syntax @ []. mutableCopy;

NSMutableArray * mArray2 = @ [@ "1", @ "2", @ "3"]. mutableCopy; // Add. mutableCopy to any variable.

NSLog (@ "% @", mArray2 );

  4. Use the smile syntax to directly assign values using the array name and subscript

MArray2 [0] =@ 100;

NSLog (@ "% @", mArray2 );

/*

13:27:55. 632 OCNSNumber [1690: 146656] (

100,

2,

3

)

*/

 

5. dictionary laughter syntax

Usually

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "xiaoZe", @ "1", @ "kunLing", @ "2", @ "sunLi", @ "3 ", nil];

NSLog (@ "% @", dic );

  Smile syntax @ {key: value ,...};

NSDictionary * dic2 =@ {@ "1": @ "xiaoZe", @ "2": @ "kunLing", @ "3": @ "sunLi "};

NSLog (@ "% @", dic2 );

 6. Variable Dictionary (can be modified only when it is variable) @ {key: value,...}. mutableCopy

NSMutableDictionary * mDic ={ @ "1": @ "a", @ "2": @ "B", @ "3": @ "c"}. mutableCopy;

NSLog (@ "% @", mDic );

[MDic setObject: @ "A" forKey: @ "1"];

NSLog (@ "% @", mDic );

 7. Modify the value corresponding to the key value

MDic [@ "2"] = @ "L ";

NSLog (@ "% @", mDic );

 

NSString * value = [mDic objectForKey: @ "2"];

NSLog (@ "% @", value );

  8. Retrieve the value corresponding to the key value

NSLog (@ "% @", mDic [@ "3"]);

The result is printed in this section:

13:31:10. 137 OCNSNumber [1700: 148140] intNumber = 100

13:31:10. 138 OCNSNumber [1700: 148140] B = 100

13:31:10. 138 OCNSNumber [1700: 148140] floatNumber = 3.14

13:31:10. 138 OCNSNumber [1700: 148140] d = 3.14

13:31:10. 139 OCNSNumber [1700: 148140] charNumber = 97

13:31:10. 139 OCNSNumber [1700: 148140] f =

13:31:10. 139 OCNSNumber [1700: 148140] Comment number = 10

13:31:10. 139 OCNSNumber [1700: 148140] h = 10

13:31:10. 139 OCNSNumber [1700: 148140] (

10,

15,

18,

22

)

13:31:10. 139 OCNSNumber [1700: 148140] 100

13:31:10. 140 OCNSNumber [1700: 148140] 100

13:31:10. 140 OCNSNumber [1700: 148140] (

ZhangSan,

LiSi,

WangEr

)

13:31:10. 140 OCNSNumber [1700: 148140] (

1,

2,

3

)

13:31:10. 140 OCNSNumber [1700: 148140] (

100,

2,

3

)

13:31:10. 140 OCNSNumber [1700: 148140] {

1 = xiaoZe;

2 = kunLing;

3 = sunLi;

}

13:31:10. 140 OCNSNumber [1700: 148140] {

1 = xiaoZe;

2 = kunLing;

3 = sunLi;

}

13:31:10. 140 OCNSNumber [1700: 148140] {

1 =;

2 = B;

3 = c;

}

13:31:10. 141 OCNSNumber [1700: 148140] {

1 =;

2 = B;

3 = c;

}

13:31:10. 155 OCNSNumber [1700: 148140] {

1 =;

2 = L;

3 = c;

}

13:31:10. 155 OCNSNumber [1700: 148140] L

13:31:10. 155 OCNSNumber [1700: 148140] c

 

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.