The following simplified notation is a new formulation after IOS6.0
NSNumber
//simplification of the previous wording:
NSNumber *value1;
value1 = [nsnumbernumberwithint:12345];
value1 = [nsnumbernumberwithfloat:123.45f];
value1 = [nsnumbernumberwithdouble:123.45];
value1 = [nsnumbernumberwithbool:YES];
//Simplified wording:
NSNumber *value2;
value2 =@12345;
value2 =@123.45f;
value2 =@123.45;
value2 =@YES;
the//boxing expression can also be written in a similar way:
nsnumber *pioversixteen1 = [nsnumbernumberwithdouble: ( m_pi / +)];
nsstring *path1 = [nsstringstringwithutf8string: getenv( "PATH")];
//Can be abbreviated as:
nsnumber *pioversixteen2 = @ ( m_pi / +);
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 look at the new wording
Nsarray *array;
Array =@[]; //Empty array
Array =@[ a ]; //An array of objects
Array =@[ A, B, C ]; //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 = [nsarrayarraywithobjects: 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
Also, 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"}; //Dictionary containing a key-value pair
dict = @{@ "Key1" : @ "value1",@ "Key2" : @ "value"@ "Key3" : @ " Value3 "}; //Dictionary with multiple key-value pairs
//The container constructed using the above notation is immutable, and if you need to generate a mutable container, you can pass the-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 Summary _ios Development section Syntax simplified notation