NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map。

通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以对应同一个值。NSDictionary 在初始化之后,就不可以再进行修改。

使用类方法创建NSDictionary对象。

初始化一个NSDictionary对象。使用+ (id)dictionaryWithObject:(id)object forKey:(id)key;

NSDictionary* dic = [NSDictionary dictionaryWithObject:@"values1" forKey:@"key1"];
NSLog(@"%@",dic);
//结果

2013-08-26 19:13:29.274 Nsdictonary[288:707] {
key1 = values1;
}

初始化一个NSDictionary对象。使用+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;

NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"values1",@"key1"
@"values2",@"key2"
@"values3",@"key3" ,nil];
NSLog(@"%@",dic);、
//结果

初始化一个NSDictionary对象。使用+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@",dic); 结果:

2013-08-26 19:30:34.286 Nsdictonary[345:707] {
key1 = values1;
key2 = values2;
key3 = values3;
}

使用实例方法创建NSDictionary

创建一个空的字典:

NSDictionary* dic = [[NSDictionary alloc]init];

NSLog(@"%@",dic);
[dic release];

通过两个数组创建字典对象。

 NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; NSLog(@"%@",dic);

通过一个字典来创建一个新的字典。

 NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2]; NSLog(@"%@",dic);

计算一个字典中有多少个键值对:

        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
NSLog(@"count :%lu",[dic count]);
NSLog(@"%@",dic1); 结果:

2013-08-26 19:44:54.809 Nsdictonary[439:707] count :3
2013-08-26 19:44:54.817 Nsdictonary[439:707] {
key1 = values1;
key2 = values2;
key3 = values3;
}

通过健来去对应的值:

 NSObject* obj = [dic objectForKey:@"key1"];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
NSObject* obj = [dic objectForKey:@"key1"];
NSLog(@"key1 = %@",obj);
结果:
2013-08-26 19:47:24.175 Nsdictonary[453:707] key1 = values1
将字典写入文件中:

 [dic writeToFile:path atomically:YES];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
NSString* path =@"/Users/administrator/Desktop/test.xml";
NSLog(@"dic:%@",dic);
[dic writeToFile:path atomically:YES];
NSDictionary* dicTest = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dicTest: %@",dicTest); 结果:

2013-08-26 19:55:31.276 Nsdictonary[500:707] dic:{
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 19:55:31.294 Nsdictonary[500:707] dicTest: {
key1 = values1;
key2 = values2;
key3 = values3;
}

返回所有的keys:

  NSArray* retKeys = [dic allKeys];:
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
NSArray* retKeys = [dic allKeys];
NSLog(@"all keys :%@",retKeys); 结果:

2013-08-26 19:58:48.871 Nsdictonary[515:707] all keys :(
key1,
key3,
key2
)

返回所有的值:allvalues

        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys]; NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
NSArray* retValues = [dic allValues];
NSLog(@"all keys :%@",retValues); 结果:

2013-08-26 19:59:57.768 Nsdictonary[532:707] all keys :(
values1,
values3,
values2
)

NSMutableDictionary  创建插入删除

 

创建一个

        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic);
结果:

2013-08-26 20:11:56.388 Nsdictonary[634:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}

插入一个新的健值对:

[dic setObject:@"values4" forKey:@"key4"];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic);
[dic setObject:@"values4" forKey:@"key4"];
NSLog(@"dic : %@",dic);
结果:

2013-08-26 20:15:36.330 Nsdictonary[680:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:15:36.338 Nsdictonary[680:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
key4 = values4;
}

移除一个健值对:

[dic removeObjectForKey:@"key1"];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic);
[dic removeObjectForKey:@"key1"];
NSLog(@"dic : %@",dic);
结果:

2013-08-26 20:17:33.980 Nsdictonary[695:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:17:34.013 Nsdictonary[695:707] dic : {
key2 = values2;
key3 = values3;
}

移除所有健值对:

removeAllObjects
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic);
[dic removeAllObjects];
NSLog(@"dic : %@",dic);
结果:

2013-08-26 20:18:38.027 Nsdictonary[711:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:18:38.036 Nsdictonary[711:707] dic : {
}

遍历字典:

for(id objects in dic)
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic);
//类似于foreach
for(id objects in dic)
{
NSObject* obj = [dic objectForKey:objects];
NSLog(@"%@ = %@",objects,obj);
}
结果:

2013-08-26 20:24:00.303 Nsdictonary[757:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:24:00.353 Nsdictonary[757:707] key1 = values1
2013-08-26 20:24:00.362 Nsdictonary[757:707] key3 = values3
2013-08-26 20:24:00.371 Nsdictonary[757:707] key2 = values2

 

迭代器遍历字典:

 NSEnumerator* em = [dic keyEnumerator];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic); NSEnumerator* em = [dic keyEnumerator];
id key =nil;
while(key = [em nextObject])
{
NSObject* obj = [dic objectForKey:key];
NSLog(@"%@ = %@",key,obj);
}

结果:

2013-08-26 20:28:23.753 Nsdictonary[771:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:28:23.871 Nsdictonary[771:707] key1 = values1
2013-08-26 20:28:23.873 Nsdictonary[771:707] key3 = values3
2013-08-26 20:28:23.879 Nsdictonary[771:707] key2 = values2

 

block遍历字典:

[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ = %@",key,obj);
}];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
NSLog(@"dic : %@",dic); [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ = %@",key,obj);
}];
结果:

2013-08-26 20:32:09.894 Nsdictonary[789:707] dic : {
key1 = values1;
key2 = values2;
key3 = values3;
}
2013-08-26 20:32:09.906 Nsdictonary[789:707] key1 = values1
2013-08-26 20:32:09.913 Nsdictonary[789:707] key3 = values3
2013-08-26 20:32:09.917 Nsdictonary[789:707] key2 = values2

 

Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary的更多相关文章

  1. Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)

    使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...

  2. Objective-C ,ios,iphone开发基础:几个常用类-NSNumber

    2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...

  3. Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)

    json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...

  4. Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。

    首先需要说明的是每个应用程序都是一个window,背景色为黑色.在window上可以跑多个view进行来回切换,下面就通过手动写代码来体现导航栏切换view的原理. 第一步,新建一个single vi ...

  5. [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  6. Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  7. Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器

    新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView  两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...

  8. Objective-C ,ios,iphone开发基础:http网络编程

    - (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...

  9. Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器

    第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...

随机推荐

  1. Eclipse与tomcat服务器建立关联

    首先,点击 打开preference,打开如下界面 点击ADD,进入如下界面,选择tomcat服务器的版本->点击next 进入如下界面,Name:服务器名字,directory:服务器目录 补 ...

  2. ES 基础

    You Know, for Search 安装es时 , jdk最低版本需要 jdk7 默认端口 : 9200 启动后浏览器访问 : localhost:9200 角色关系对照 elasticsear ...

  3. 如何使用git创建远程仓库(供局域网多人使用)

    用git init(默认创建的是私人的仓库)创建的仓库,推送是不会成功的. 因此在git server端,我们要用 git --bare init --shared=group 来创建一个bare库, ...

  4. Web Service 与 EJB 的分布式的区别

    EJB的分布式:一个业务逻辑可能会调用分布在多台服务器上的 EJB 组件,但是这么多的组件调用必须纳入一个事务范围之中.也就是说如果需要调用三个 EJB 组件,第一个调用成功,第二个调用成功,但第三个 ...

  5. Java集合框架之LinkedList-----用LinkedList模拟队列和堆栈

    LinkedList的特有方法: (一)添加方法 addFisrt(E e):将指定元素插入此列表的开头.//参数e可以理解成Object对象,因为列表可以接收任何类型的对象,所以e就是Object对 ...

  6. Spring Filter components in auto scanning

    In this Spring auto component scanning tutorial, you learn about how to make Spring auto scan your c ...

  7. VIM技巧(1)

    VIM技巧(1) 替换 36s/^\(.* = \)entity.\(.*\)$/\1this.GetShowName("\2",\2); 删除空行 %g/^$/d %g/^\s* ...

  8. SOP、DIP、PLCC、TQFP、PQFP、TSOP、BGA封装解释

    1. SOP封装SOP是英文Small Outline Package的缩写,即小外形封装.SOP封装技术由1968-1969年菲利浦公司开发成功,以后逐渐派生出SOJ(J型引脚小外形封装).TSOP ...

  9. Unity3D之UGUI学习笔记(一):UGUI介绍以及Canvas

    UGUI是Unity3D4.6官方提供的UI系统,支持2D和3D UI的开发. Unity3D UI史 OnGUI 在Unity4.6之前,官方提供的是OnGUI函数来开发UI界面,当然问题也比较多, ...

  10. Jstl标签的使用

    一. 配置 JSTL 包括两个 JAR 文件, jstl.jar 和 standard.jar .是什么没有必要管,重在应用( 1+1 ? =2 ,我们没有必要深究,只需要知道这么用就行.). 原文引 ...