Ext.: http://www.2cto.com/kf/201408/328974.html
The following simplified notation is a new formulation after IOS6.0
NSNumber
To simplify the previous wording:
NSNumber *value1;
value1 = [NSNumber numberwithint:12345];
value1 = [NSNumber numberwithfloat:123.45f];
value1 = [NSNumber numberwithdouble:123.45];
value1 = [NSNumber numberwithbool:yes];
The simplified wording:
NSNumber *value2;
value2 = @12345;
value2 = @123.45f;
value2 = @123.45;
value2 = @YES;
Boxing expressions can also be written in similar notation:
NSNumber *pioversixteen1 = [NSNumber numberwithdouble: (M_PI/16)];
NSString *path1 = [NSString stringwithutf8string:getenv ("PATH")];
can be abbreviated as:
NSNumber *pioversixteen2 = @ (M_PI/16);
NSString *path2= @ (getenv ("PATH"));
For string expressions, it is important to note that the value of an expression must not be null, or an exception will be thrown.
Nsarray
For Nsarray initialization, there are a lot of writing, here is no longer one by one listed, we directly see the new wording
Nsarray *array;
Array = @[]; Empty array
Array = @[A]; An array of objects
Array = @[A, b, c]; An array of multiple objects
When the compiler is processing:
Array = @[A, b, c];
Compiler-Generated code:
ID objects[] = {A, b, c};
Nsuinteger count = sizeof (objects)/sizeof (ID);
Array = [Nsarray arraywithobjects:objects count:count];
Note that if the A,b,c object has nil, the runtime throws an exception, which is different from the original processing, so be careful when coding.
Nsdictionary
Similarly, for a dictionary of this data structure, there are many ways to initialize, we look at the new wording:
Nsdictionary *dict;
Dict = @{}; Empty dictionary
Dict = @{@ "Key1": @ "value1"}; A dictionary that contains a key-value pair
Dict = @{@ "Key1": @ "value1", @ "Key2": @ "value" @ "Key3": @ "Value3"}; Dictionary with multiple key-value pairs
Containers built using the above notation are immutable, and if you need to generate a mutable container, you can pass a-mutablecopy message. For example
Nsmutablearray *mutableplanets = [@[
@ "Mercury", @ "Venus", @ "Earth",
@ "Mars", @ "Jupiter", @ "Saturn",
@ "Uranus", @ "Neptune"
] Mutablecopy];
Nsmutabledictionary *dic=[@{@ "Key1": @ "value1", @ "Key2": @ "value" @ "Key3": @ "Value3"} Mutablecopy];
IOS 6 new shortcut initialization notation