*********************************************
NSDictionary
*********************************************
// 字典的概念:
// 键值对的概念:
// 查字典时: 用一个键,得到一个值
字典的构造方法:
NSDictionary * dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
NSLog(@"dict1: %@", dict1);
// 字典的字面常量对象:
// 1, 1,3, "abc", @"abc", @[@"abc",@""] @{@"key":@"value",@"k2":@"v2"}
// NSDictionary * dict2 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
#define DICT @{@"1":@"one",@"2":@"two",@"3":@"three"}
NSDictionary * dict2 = DICT;//DICT是一个宏
NSLog(@"dict2: %@",dict2);
NSDictionary * dict3 = [[ NSDictionary alloc]initWithDictionary:dict2];
NSLog(@"dict3: %@", dict3);
NSArray * keys =@[@"key1",@"key2",@"key3"];
NSArray * values =@[@"value1",@"value2",@"value3"];
NSDictionary * dict4 = [[ NSDictionary alloc]initWithObjects:values forKeys:keys];
NSLog(@"dict4: %@", dict4);
*********************************************
//根据键查值
NSDictionary * dict1 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
NSString * key = @"1";
// 根据一个键,得到一个值,如果字典中没有这个键值对,则objectForKey:这个方法返回一个nil
NSString *value = [dict1 objectForKey:key];
NSString *value2 = dict1[key];
NSString *value3 = dict1[@"2"];
//从终端获取一个key,得到一个值
char buf[128];
// fgets 和 gets的区别:
// gets:从终端的输入缓冲区不断地获取字符存入buf,直到遇到回车符结束,
// 但是最后的那个回车符不会存入buf,只会往buf后边补一个' '
// -> abc buf: abc
// fgets(buf, 122, stdin): 从终端的输入缓冲区不断地获取字符,但最多只能获取122个字符,如果不够122个字符时就遇到了回车符,那么回车符也
// 会放入buf, 且在回车符后边再补一个' '
// -> abc buf: abc
while (1) {
NSLog(@"请输入一个key:");
#if 0
fgets(buf, 122, stdin);
buf[strlen(buf)-1]=' ';
#else
gets(buf);
#endif
NSString *inputKey = [NSString stringWithUTF8String:buf];
NSString * getValue = [dict1 objectForKey:inputKey];
NSLog(@"值为: %@", getValue);
}
*********************************************
//遍历字典
NSDictionary * dict1 = @{@"33":@"three",@"1":@"one",@"2":@"two",@"3":@"three"};
// 获取字典所有的键
NSArray *keys=[dict1 allKeys];
// 获取字典所有的值
NSArray *values = [dict1 allValues];
id key;
//快速遍历字典的所有键
for(key in dict1)
{
NSLog(@"key: %@",key);
NSLog(@"value: %@", dict1[key]);
}
//注意: 从字典获取的枚举器枚举的是字典的所有值,而不是键
NSEnumerator *enumerator = [dict1 objectEnumerator];
id obj;
while (obj=[enumerator nextObject])
{
NSLog(@"obj: %@", obj);
//注意:词典中:值可以相同,但键不能相同,即一个值可以有多个键
NSArray * keysOfObj= [dict1 allKeysForObject:obj];
NSLog(@"某个值对应的所有键为:%@",keysOfObj);
}
*********************************************
// 其他方法:
//1, 获取字典的键值对数量
NSDictionary * dict1 = DICT;
NSUInteger count = [dict1 count];
NSLog(@"字典一共有%lu个键值对", count);
*********************************************
*********************************************
NSMutableDictionary
*********************************************
可变字典的构造方法:
//构造一个空字典
NSMutableDictionary *mdict = [NSMutableDictionary dictionary];
NSMutableDictionary *mdict1=[[NSMutableDictionary alloc]init];
NSMutableDictionary * mdict2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil];
NSMutableDictionary * mdict3 = [[ NSMutableDictionary alloc]initWithObjects:@[@"vx1",@"vx2",@"vx3"] forKeys:@[@"kx1",@"kx2",@"kx3"]];
*********************************************
添加键值对:
NSMutableDictionary *mdict1=[[NSMutableDictionary alloc]init];
[mdict1 setObject:@"setV1" forKey:@"setK1"];
[mdict1 setObject:@"setV1" forKey:@"setK2"];
NSLog(@"mdict1: %@", mdict1);
NSLog(@"两个值的地址分别为:%p %p", mdict1[@"setK1"], mdict1[@"setK2"]);
NSLog(@"两个abc的地址分别为: %p %p", @"abc", @"abc");
*********************************************
修改键值对:
NSMutableDictionary * mdict1 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil];
//现在时刻,mdict1 : k1=v1 k2=v2
// 修改某一个键对应的值, 让键 k2的值不再是 v2
[mdict1 setObject:@"this is newValue for k2" forKey:@"k2"];
NSLog(@"mdict1: %@", mdict1);
*********************************************
删除键值对:
NSMutableDictionary * mdict2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2",@"v3",@"k3", @"v5",@"k5",@"v6",@"k6",nil];
NSLog(@"原始字典: mdict2: %@", mdict2);
//删除一个键值对,需要传入一个键
[mdict2 removeObjectForKey:@"k2"];
NSLog(@"删除一个键对应的键值对后:mdict2: %@", mdict2);
//删除一组键对应的键值对, 传入一个键的数组
[mdict2 removeObjectsForKeys:@[@"k5",@"k6"]];
NSLog(@"删除一组键对应的键值对后:mdict2: %@", mdict2);
//重置字典,即原来的所有键值对无效了。
[mdict2 setDictionary:@{@"newKey1":@"newValue1",@"newKey2":@"newValue2"}];
//删除所有键值对
[mdict2 removeAllObjects];
NSLog(@"删除所有键值对后:mdict2: %@", mdict2);
*********************************************
*********************************************
*********************************************