- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 字面量語法、、、、縮減源代碼長度,更易閱讀 // 傳統建立方式是指alloc init 方法 // 字符串字面量 NSString *string = @"Objective -2.0"; // 字面數值 NSNumber *intNumber = @1; NSNumber *floatNumber1 = @1.01f; NSNumber *doubleNumber = @3.14159; NSNumber *boolNumber = @YES; NSNumber *charNumber = @'j'; int x = 5; float y = 6.32f; NSNumber *NumberX = @(x * y); NSLog(@"%@\n",NumberX); // 字面量數組 // NSArray *numberArray = [NSArray arrayWithObjects:intNumber,floatNumber1,doubleNumber,boolNumber,charNumber,NumberX, nil]; // 數組元素中有對象爲nil,會拋出異常 NSArray *numberArray = @[intNumber,floatNumber1,doubleNumber,boolNumber,charNumber,NumberX]; NSArray *stringArray = @[string,@"cat",@"dog",@"me"]; string = stringArray[0]; floatNumber1 = numberArray [1];//取下標 NSLog(@"%@\n,%@\n",floatNumber1,string); // 字面量字典 // NSDictionary *personData = [NSDictionary dictionaryWithObjectsAndKeys:@"Zou",@"FirstName",@"Jie",@"LastName",[NSNumber numberWithInt:28],@"age", nil]; // *********字典大括號************** NSDictionary *personData = @{@"FirstName":@"Zou",@"LastName":@"Jie",@"age":@28}; NSString *FirstName = personData[@"FirstName"]; NSLog(@"%@\n",FirstName); // 可變數組與字典 能夠經過取下標直接修改元素 NSMutableArray *mutableArray = [[NSMutableArray alloc]init]; // 數組元素第二個爲nil,程序崩潰 // mutableArray[1] = @"dog"; mutableArray[0]=@"dog"; NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc]initWithDictionary:personData]; mutableDic[@"FirstName"] = @"Zhou"; NSLog(@"%@\n",mutableDic[@"FirstName"]); // 字面量語法建立的對象都是不可變的,可變版本則須要複製一份; NSMutableArray *mutable = [@[@1,@2,@3,@4]mutableCopy]; NSLog(@"%@\n",mutable); }