Java有Map,可以把数据以键值对的形式储存起来,取值的时候通过key就可以直接拿到对应的值,方便快捷。在Objective-C语言中,词典就是做这样的事情的,和NSArray一样,一个词典对象也能保存不同类型的值,词典也分别有不可变词典和可变的词典(NSDictionary与NSMutableDictionary),前者是线程安全的,后者不是 。
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7088/57422bd0-f67e-35fa-9788-6eed7f194cc6.jpg[/img]
[/img]
1、不可变词典NSDictionary的主要用法:
[NSDictionary dictionaryWithObjectsAndKeys:..] : 使用键值对直接创建词典对象,结尾必需使用nil标志结束。
[dictionary count]: 得到词典的键值对数量。
[dictionary keyEnumerator]: 将词典的所有key储存在NSEnumerator中,类似于Java语言中的迭代器
[dictionary objectEnumerator]: 将词典的所有value储存在NSEnumerator中
[dictionary objectForKey:key]: 通过传入key对象可以拿到当前key对应储存的值。
代码示例:
打印结果;
2012-07-09 16:31:18.276 objectiveC[2965:403] 3
2012-07-09 16:31:18.282 objectiveC[2965:403] key:age
2012-07-09 16:31:18.282 objectiveC[2965:403] key:name
2012-07-09 16:31:18.283 objectiveC[2965:403] key:性别
2012-07-09 16:31:18.283 objectiveC[2965:403] value:25
2012-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的数据
代码示例:
打印结果:
2012-07-09 16:37:07.037 objectiveC[3053:403] 4
2012-07-09 16:37:07.042 objectiveC[3053:403] key:age
2012-07-09 16:37:07.043 objectiveC[3053:403] key:性别
2012-07-09 16:37:07.043 objectiveC[3053:403] key:name
2012-07-09 16:37:07.044 objectiveC[3053:403] key:名次
2012-07-09 16:37:07.044 objectiveC[3053:403] value:25
2012-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名
[img]
[img]http://dl.iteye.com/upload/attachment/0078/7088/57422bd0-f67e-35fa-9788-6eed7f194cc6.jpg[/img]
[/img]
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] 3
2012-07-09 16:31:18.282 objectiveC[2965:403] key:age
2012-07-09 16:31:18.282 objectiveC[2965:403] key:name
2012-07-09 16:31:18.283 objectiveC[2965:403] key:性别
2012-07-09 16:31:18.283 objectiveC[2965:403] value:25
2012-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] 4
2012-07-09 16:37:07.042 objectiveC[3053:403] key:age
2012-07-09 16:37:07.043 objectiveC[3053:403] key:性别
2012-07-09 16:37:07.043 objectiveC[3053:403] key:name
2012-07-09 16:37:07.044 objectiveC[3053:403] key:名次
2012-07-09 16:37:07.044 objectiveC[3053:403] value:25
2012-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名