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. python setup.py install 失败

    由于curl证书太老,所以无法找到一些对应的版本. 如下更新证书即可: curl http://curl.haxx.se/ca/cacert.pem > /etc/pki/tls/certs/c ...

  2. as3 中trace() 函数对效率的影响

    进行页游开发的过程中,很多开发者都有一个习惯,在数据输出中添加trace()函数来跟踪数值 - 不进行条件编译,发布的时候也不删除.实际上大量的trace函数会降低程序的效率,我们可以用一个简单的例子 ...

  3. Makefile中用宏定义进行条件编译

    在源代码里面如果这样是定义的: #ifdef   MACRONAME //可选代码 #endif 那在makefile里面 gcc   -D   MACRONAME=MACRODEF 或者 gcc   ...

  4. struts2+Hibernate4+spring3+EasyUI环境搭建之四:引入hibernate4以及spring3与hibernate4整合

    1.导入hibernate4 jar包:注意之前引入的struts2需要排除javassist  否则冲突 <!-- hibernate4 --> <dependency> & ...

  5. 【转】从零开始,让你的框架支持CocoaPods

    首先概括一个大概的步骤: 代码上传到Github 创建podspec文件 在Github上创建release版本 注册CocoaPods账号 上传代码到CocoaPods 检验是否上传成功 更新框架版 ...

  6. 查看解决Oracle对象锁住的问题

    在编译的一个存储过程的时候,对象可能由于被锁住而处于假的卡死状态,这个时候有的是因为,这个过程正在运行中,所以无法编译: 上次我遇到一次,我很清楚的确定这个过程没有运行,可以我就是无法编译,对象一直被 ...

  7. POJ 1308 Is It A Tree? (并查集)

    Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...

  8. HTTP协议状态码详解

    HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了.  如果是做AJAX,REST,网络爬虫, ...

  9. final static T

    /** * An empty table instance to share when the table is not inflated. */ static final Entry<?,?& ...

  10. 图文讲解:iOS App提交流程

    原文:http://www.toceansoft.com/ios/3287.jhtml 一.证书的导出 1.1.前期工作 首先你需要有一个苹果的开发者帐号,一个Mac系统. 如果没有帐号可以在打开ht ...