Java有Map,可以把数据以键值对的形式储存起来,取值的时候通过key就可以直接拿到对应的值,方便快捷。在Objective-C语言中,词典就是做这样的事情的,和NSArray一样,一个词典对象也能保存不同类型的值,词典也分别有不可变词典和可变的词典(NSDictionary与NSMutableDictionary),前者是线程安全的,后者不是 。
1、不可变词典NSDictionary的主要用法:
[NSDictionary dictionaryWithObjectsAndKeys:..] : 使用键值对直接创建词典对象,结尾必需使用nil标志结束。
[dictionary count]: 得到词典的键值对数量。
[dictionary keyEnumerator]: 将词典的所有key储存在NSEnumerator中,类似于Java语言中的迭代器 [dictionary objectEnumerator]: 将词典的所有value储存在NSEnumerator中 [dictionary objectForKey:key]: 通过传入key对象可以拿到当前key对应储存的值。代码示例:
int main(int argc, const char * argv[]){ @autoreleasepool { NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"张三",@"name",@"男",@"性别",nil]; NSLog(@"%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 name的值是:%@", [dictionary objectForKey:@"name"]); } return 0;}
打印结果;
2012-07-09 16:31:18.276 objectiveC[2965:403] 32012-07-09 16:31:18.282 objectiveC[2965:403] key:age2012-07-09 16:31:18.282 objectiveC[2965:403] key:name2012-07-09 16:31:18.283 objectiveC[2965:403] key:性别2012-07-09 16:31:18.283 objectiveC[2965:403] value:252012-07-09 16:31:18.284 objectiveC[2965:403] value:张三2012-07-09 16:31:18.284 objectiveC[2965:403] value:男2012-07-09 16:31:18.285 objectiveC[2965:403] key name的值是:张三
2、可变的词典NSMutableDictionary。
NSMutableDictionary是NSDictionary的子类,所以继承了NSDictionary的方法, 以上的代码对NSMutableDictionary来说完全可用。我们试试不一样的地方
增删键值数据。
[dictionary setObject: forKey:] :向可变的词典动态的添加数据
[dictionary removeAllObjects..] : 删除掉词典中的所有数据。 [dictionary removeObjectForKey..] :删除掉词典中指定key的数据代码示例:
int main(int argc, const char * argv[]){ @autoreleasepool { NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"张三",@"name",@"男",@"性别",nil]; [dictionary setObject:@"30名" forKey:@"名次"]; NSLog(@"%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]); } return 0;}
打印结果:
2012-07-09 16:37:07.037 objectiveC[3053:403] 42012-07-09 16:37:07.042 objectiveC[3053:403] key:age2012-07-09 16:37:07.043 objectiveC[3053:403] key:性别2012-07-09 16:37:07.043 objectiveC[3053:403] key:name2012-07-09 16:37:07.044 objectiveC[3053:403] key:名次2012-07-09 16:37:07.044 objectiveC[3053:403] value:252012-07-09 16:37:07.045 objectiveC[3053:403] value:男2012-07-09 16:37:07.045 objectiveC[3053:403] value:张三2012-07-09 16:37:07.046 objectiveC[3053:403] value:30名2012-07-09 16:37:07.046 objectiveC[3053:403] key 名次的值是:30名
著作权声明:本文由原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!