//key 和 value 都屬於(id)對象類型xcode
//key經常使用字符串NSString來表示code
//存儲數值型 通常可用 NSString對象
//int age ->@(age)rem
// [dic[name] intValue];字符串
// @()-->轉成NSNumber 類型it
// intValueio
//value能夠使任意對象(NSString NSArrray , dog )table
//NSDictionary 一旦建立不能修改增長刪除object
#pragma mark 字典的建立查詢
NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:
@"value1",@"key1",
@"value2",@"key2",
@"value2",@"key3",nil];
NSLog(@"%@",dic);
NSDictionary * dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1", nil];
//用字典建立字典
NSDictionary * dic2 = [[NSDictionary alloc]initWithDictionary:dic];
NSLog(@"%@",dic2);
//xcode4.6
//格式: key : value 多個元素之間用,分隔
//用NSLog打印字典是不支持漢字的轉碼
// \U5bd2\U4ed2;
NSDictionary * dic3 =
@{@"key1":@"value1",
@"key2":@"value2",
@"key3":@"value3",
@"key4":@"value2"
};
#pragma mark 查看鍵的個數
NSUInteger num = [dic2 count];
NSLog(@"%lu",num);
#pragma mark 經過key找value
NSString * str = [dic3 objectForKey:@"key1"];
//xcode4.6
NSString * str2 = dic3[@"key2"];
NSLog(@"%@",str);
NSLog(@"%@",str2);
#pragma mark 獲取全部key 全部value
NSArray * keyArray = [dic3 allKeys];
NSArray * valueArray = [dic3 allValues];
NSLog(@"keys = %@",keyArray);
NSLog(@"values = %@",valueArray);
#pragma mark 查看一個value對應多個key
NSArray * array = [dic3 allKeysForObject:@"value2"];
NSLog(@"%@",array);
#pragma mark 可變字典
#pragma mark 建立可變字典
NSMutableDictionary * mutDic = [[NSMutableDictionary alloc] init];
NSDictionary * dic4 =
@{@"key1":@"value1",
@"key2":@"value2",
@"key3":@"value3",
@"key4":@"value2"
};
NSMutableDictionary * mutDic2 = [[NSMutableDictionary alloc]initWithDictionary:dic4];
#pragma mark 增長
//key不重複是增長鍵值對
[mutDic2 setObject:@"value3"
forKey:@"key5"];
//保證Key不重複 key必須惟一
//經過查詢key的obj 是否爲 ==NULL
// if ([mutDic2 objectForKey:@"key5"]== NULL) {
//
// }
NSDictionary * dic5 = @{@"key_1":@"value1",
@"key_2":@"value2"};
[mutDic2 addEntriesFromDictionary:dic5];
NSLog(@"%@",mutDic2);
#pragma mark 刪除
//經過key刪除值
[mutDic2 removeObjectForKey:@"key_1"];
NSLog(@"%@",mutDic2);
NSArray * array2 = @[@"key_2",@"key2",@"key4"];
[mutDic2 removeObjectsForKeys:array2];
NSLog(@"%@",mutDic2);
// [mutDic2 removeAllObjects];
// NSLog(@"%@",mutDic2);
#pragma mark 修改
//key 已存在是修改當前value的值
[mutDic2 setObject:@"hhhh" forKey:@"key1"];
NSLog(@"%@",mutDic2);
//徹底覆蓋掉當前字典中全部的值
[mutDic2 setDictionary:dic2];
NSLog(@"%@",mutDic2);
}