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. USACO 2013 November Contest Gold 简要题解

    Problem 1. Empty Stalls 扫两遍即可. Problem 2. Line of Sight 我们发现能互相看见的一对点一定能同时看见粮仓的某一段.于是转换成有n段线段,问有多少对线 ...

  2. scribe、chukwa、kafka、flume日志系统对比 -摘自网络

    1. 背景介绍许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征:(1) 构建应用系统和分析系统的桥梁 ...

  3. B+树|MYSQL索引使用原则

    MySQL一直了解得都不多,之前写sql准备提交生产环境之前的时候,老员工帮我检查了下sql,让修改了一下存储引擎,当时我使用的是Myisam,后面改成InnoDB了.为什么要改成这样,之前都没有听过 ...

  4. homework-03

    1.分工准备 这次的工作是结对编程,在第二次作业中我是使用python完成的作业,而小明是使用C完成的作业.因为打算使用动态链接库的方式将第二次的代码嵌入到本次的作业中,而python生成动态链接库不 ...

  5. mongod的主要参数解释

    mongod的主要参数有:

  6. UIButton上使用UIEdgeInsetsMake让title跟图片对齐

    UIButton上使用UIEdgeInsetsMake让title跟图片对齐 默认情况下,不设置的效果,都使居中现实,button为150*150 使用以下设置后:   [self setTitleE ...

  7. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  8. 在Linux下部署activemq

    今天的任务就是在一台新的服务器上继续部署activemq.其实都蛮简单的.首先先下载包:115U盘下载 2 上传到linux下的某个文件夹下.解压缩 tar -zxvf apache-activemq ...

  9. mysql删除重复记录语句,删除除了 id 号不同,其他都相同的学生冗余信息

    /** 在Mysql下执行: delete from my.stu where id not in( select min(id) id from my.stu group by code) ; 用途 ...

  10. PHP 中运用 elasticsearch

    PHP扩展安装 1. 环境要求:PHP_VERSION >= 5.3.9,composer工具 2. 在E盘新建文件夹命名为elastic,,拷贝composer.phar到      E:/e ...