IOS summary_simplified syntax for IOS development
The following simplified method is the new method after IOS6.0
NSNumber
// Simplify the preceding statement:
NSNumber * value1;
Value1 = [NSNumbernumberWithInt: 12345];
Value1 = [NSNumbernumberWithFloat: 123.45f];
Value1 = [NSNumbernumberWithDouble: 123.45];
Value1 = [NSNumbernumberWithBool: YES];
// Simplified statement:
NSNumber * value2;
Value2 = @ 12345;
Value2 = @ 123.45f;
Value2 = @ 123.45;
Value2 = @ YES;
// The packing expression can also be written in a similar way:
NSNumber * piOverSixteen1 = [NSNumbernumberWithDouble: (M_PI/16)];
NSString * path1 = [nsstringstringwithuf8string: getenv ("PATH")];
// Can be abbreviated:
NSNumber * piOverSixteen2 = @ (M_PI/16 );
NSString * path2 = @ (getenv ("PATH "));
// For string expressions, note that the expression value must not be NULL; otherwise, an exception is thrown.
NSArray
// For NSArray initialization, there are a lot of writing methods, which will not be listed here. Let's look at the new writing method directly.
NSArray * array;
Array = @ []; // empty array
Array = @ [a]; // an array of Objects
Array = @ [a, B, c]; // array of multiple objects
// Compiler processing:
Array = @ [a, B, c];
// The code generated by the compiler:
Id objects [] = {a, B, c };
NSUInteger count = sizeof (objects)/sizeof (id );
Array = [NSArrayarrayWithObjects: objects count: count];
// Note that if the, B, and c objects have nil, an exception will be thrown during running. This is different from the original processing method. Be careful when coding.
NSDictionary
// Similarly, there are many initialization methods for the data structure of the dictionary. Let's look at the new method:
NSDictionary * dict;
Dict =@{}; // empty dictionary
Dict =@{@ "key1": @ "value1"}; // dictionary containing a key-Value Pair
Dict = @ {@ "key1": @ "value1", @ "key2": @ "value", @ "key3": @ "value3 "}; // dictionary containing multiple key-value pairs
// The containers built using the preceding method are immutable. To generate a variable container, You can transmit 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];