介绍

Java有Map,可以把数据以键值对的形式储存起来,取值的时候通过key就可以直接拿到对应的值,方便快捷。在Objective-C语言中,词典就是做这样的事情的,和NSArray一样,一个词典对象也能保存不同类型的值,词典也分别有不可变词典和可变的词典(NSDictionary与NSMutableDictionary),前者是线程安全的,后者不是 。

字典

NSDictionary不可变字典

  • [NSDictionary dictionaryWithObjectsAndKeys:..] : 使用键值对直接创建词典对象,结尾必需使用nil标志结束。

void doNSDictionary() {
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"liuguoquan",@"name",@"male",@"sex", nil];
    NSLog(@"dict count: %ld",[dict count]);
    
    //迭代遍历
    NSEnumerator *enmuerator = [dict keyEnumerator];
    id obj;
    while (obj = [enmuerator nextObject]) {
        NSLog(@"字典元素: %@",obj);
    }
    
    //所有键值集合
    NSArray *key = [dict allKeys];
    for(NSObject *object in key) {
        NSLog(@"值: %@.",[dict objectForKey:object]);
    }
    
}

2016-10-10 11:32:22.856639 DictionaryDemo[17399:1378107] dict count: 3
2016-10-10 11:32:22.857286 DictionaryDemo[17399:1378107] 字典元素: age
2016-10-10 11:32:22.857317 DictionaryDemo[17399:1378107] 字典元素: name
2016-10-10 11:32:22.857330 DictionaryDemo[17399:1378107] 字典元素: sex

2016-10-10 11:32:22.857372 DictionaryDemo[17399:1378107] 值: 25.
2016-10-10 11:32:22.857390 DictionaryDemo[17399:1378107] 值: liuguoquan.
2016-10-10 11:32:22.857404 DictionaryDemo[17399:1378107] 值: male.

NSMutableDictionary可变字典

NSMutableDictionary是NSDictionary的子类,所以继承了NSDictionary的方法, 以上的代码对NSMutableDictionary来说完全可用。下面来看看不一样的增删键值数据

  • [dictionary setObject: forKey:] :向可变的词典动态的添加数据

  • [dictionary removeAllObjects..] : 删除掉词典中的所有数据。

  • [dictionary removeObjectForKey..] :删除掉词典中指定key的数据

void doNSMutableDictionary() {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"张三",@"name",@"男",@"性别",nil];
    //增加键值对
    [dictionary setObject:@"30名" forKey:@"名次"];
    
    NSLog(@"mutDict count: %lu", [dictionary count]);
    NSEnumerator *enumeratorKey = [dictionary keyEnumerator];
    for (NSObject *object in enumeratorKey) {
        NSLog(@"key:%@", object);
    }
    
    NSEnumerator *enumeratorObject = [dictionary objectEnumerator];
    for (NSObject *object in enumeratorObject) {
        NSLog(@"value:%@", object);
    }
    NSLog(@"key 名次的值是:%@", [dictionary objectForKey:@"名次"]);
    [dictionary removeObjectForKey:@"名次"];
    NSLog(@"%lu", [dictionary count]);
}

2016-10-10 11:32:22.857428 DictionaryDemo[17399:1378107] mutDict count: 4
2016-10-10 11:32:22.857460 DictionaryDemo[17399:1378107] key:age
2016-10-10 11:32:22.857485 DictionaryDemo[17399:1378107] key:性别
2016-10-10 11:32:22.857497 DictionaryDemo[17399:1378107] key:name
2016-10-10 11:32:22.857508 DictionaryDemo[17399:1378107] key:名次

2016-10-10 11:32:22.857533 DictionaryDemo[17399:1378107] value:25
2016-10-10 11:32:22.857543 DictionaryDemo[17399:1378107] value:男
2016-10-10 11:32:22.857552 DictionaryDemo[17399:1378107] value:张三
2016-10-10 11:32:22.857561 DictionaryDemo[17399:1378107] value:30名

2016-10-10 11:32:22.857594 DictionaryDemo[17399:1378107] key 名次的值是:30名
2016-10-10 11:32:22.857635 DictionaryDemo[17399:1378107] 3

刘涤生
243 声望29 粉丝

临渊羡鱼,不如退而结网。