码迷,mamicode.com
首页 > 其他好文 > 详细

[Objective-C] 009_Foundation框架之NSDictionary与NSMutableDictionary

时间:2015-06-23 00:45:07      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

  在Cocoa Foundation中NSDictionary和NSMutableDictionary 用于对象有序集合,NSDictionary和NSMutableDictionary 与 NSArray和NSMutableArray 有些不一样,前者可以保存多个不同类型的数据,而后者只能保存同一类型的数据。

1.NSDictionary 创建

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];

2.NSDictionary 遍历

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];

//得到词典的数量
int objectCount = (int)[dictionary count];
NSLog(@"词典的KEY数量为: %d",objectCount);

//获取词典中所有KEY值
NSEnumerator * enumeratorKey = [dictionary keyEnumerator];
//快速枚举遍历所有KEY的值
for (NSObject *object in enumeratorKey) {
    NSLog(@"KEY的值---》: %@",object);
}

//获取词典中所有Value值
NSEnumerator * enumeratorValue = [dictionary objectEnumerator];
//快速枚举遍历所有Value的值
for (NSObject *object in enumeratorValue) {
    NSLog(@"Value的值---》: %@",object);
}

3.NSDictionary 通过KEY找到的value

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];
NSObject *object = [dictionary objectForKey:@"teamName"];
if (object != nil) {
    NSLog(@"通过KEY找到的value是: %@",object);
}

4.NSMutableDictionary 创建

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10];

5.NSMutableDictionary 元素操作

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10];
    
//向词典中动态添加数据
[dictionary setObject:@"SuperDo" forKey:@"teamName"];
[dictionary setObject:@"SuperDo.Horse" forKey:@"teamMember"];

//通过KEY找到value
NSObject *object = [dictionary objectForKey:@"teamName"];

//获取所有的key
NSArray *allKeys = [dictionary allKeys];

//获取所有的value
NSArray *allValues = [dictionary allValues];

 

技术分享 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4594337.html

 

 

[Objective-C] 009_Foundation框架之NSDictionary与NSMutableDictionary

标签:

原文地址:http://www.cnblogs.com/superdo/p/4594337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!