ios高效開發-使用字面量語法讓iOS代碼更簡潔漂亮

NSNumber *nInt = @1;
    NSNumber *nFloat = @1.1;
    NSNumber *nDouble = @1.1111111;
    NSNumber *nBool = @YES;
    NSNumber *nChar = @'a';
    
    NSLog(@"nInt : %@",nInt);
    NSLog(@"nFloat : %@",nFloat);
    NSLog(@"nDouble : %@",nDouble);
    NSLog(@"nBool : %@",nBool);
    NSLog(@"nChar : %@",nChar);
    
    int a = 5;
    int b = 6;
    NSNumber *nExpression = @(a*b);
    NSLog(@"nExpression : %@",nExpression);
    
    NSArray *array1 = @[@"1",@"2",@"3",@"4",@"5"];
    NSArray *array2 = @[@1,@2,@3,@4,@5];
    NSLog(@"array1 : %@",array1);
    NSLog(@"array2 : %@",array2);
    
    NSLog(@"array1-1 : %@",array1[1]);
    NSLog(@"array2-2 : %@",array2[2]);
    
    NSDictionary *dict1 = @{@"name":@"wangdalei",@"age":@26,@"sex":@"男"};
    NSLog(@"dict1 : %@",dict1);
    NSLog(@"dict1[@\"name\"] : %@",dict1[@"name"]);
    
    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]initWithDictionary:@{@"name":@"wangdalei",@"age":@26,@"sex":@"男"}];
    dict2[@"name"] = @"moxi90";
    dict2[@"age"] = @90;
    
    NSLog(@"dict2[@\"name\"] :%@",dict2[@"name"]);
    NSLog(@"dict2[@\"age\"] : %@",dict2[@"age"]);


輸出日誌:


2016-01-25 14:51:39.136 WDLClock[2728:396830] nInt : 1
2016-01-25 14:51:39.136 WDLClock[2728:396830] nFloat : 1.1
2016-01-25 14:51:39.137 WDLClock[2728:396830] nDouble : 1.1111111
2016-01-25 14:51:39.137 WDLClock[2728:396830] nBool : 1
2016-01-25 14:51:39.137 WDLClock[2728:396830] nChar : 97
2016-01-25 14:51:39.137 WDLClock[2728:396830] nExpression : 30
2016-01-25 14:51:39.137 WDLClock[2728:396830] array1 : (
    1,
    2,
    3,
    4,
    5
)
2016-01-25 14:51:39.138 WDLClock[2728:396830] array2 : (
    1,
    2,
    3,
    4,
    5
)
2016-01-25 14:51:39.138 WDLClock[2728:396830] array1-1 : 2
2016-01-25 14:51:39.138 WDLClock[2728:396830] array2-2 : 3
2016-01-25 14:51:39.138 WDLClock[2728:396830] dict1 : {
    age = 26;
    name = wangdalei;
    sex = "\U7537";
}
2016-01-25 14:51:39.138 WDLClock[2728:396830] dict1[@"name"] : wangdalei
2016-01-25 14:51:39.138 WDLClock[2728:396830] dict2[@"name"] :moxi90
2016-01-25 14:51:39.139 WDLClock[2728:396830] dict2[@"age"] : 90


再添加一個關於Mutable的變量的聲明:

NSMutableArray *ar3 = [@[@"2",@"6",@"5",@"4",@"3"] mutableCopy];
NSMutableDictionary *dic3 = [@{@"xxxx":@"2",@"wwww":@"w",@"eeee":@"e",@"rrrr":@"r"} mutableCopy];





發佈了88 篇原創文章 · 獲贊 18 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章