Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary
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的更多相关文章
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。
首先需要说明的是每个应用程序都是一个window,背景色为黑色.在window上可以跑多个view进行来回切换,下面就通过手动写代码来体现导航栏切换view的原理. 第一步,新建一个single vi ...
- [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器
新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView 两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...
- Objective-C ,ios,iphone开发基础:http网络编程
- (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...
- Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器
第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...
随机推荐
- socket.io的抽象实现:engine.io
engine.io是一个socket.io的抽象实现,作为socket.io的服务器和浏览器之间交换的数据的传输层.它不会取代Socket.IO,它只是抽象出固有的复杂性,支持多种浏览器,设备和网络的 ...
- Hibernate初认识以及HelloWorld
一.Hibernate初认识 1. Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. 2.对 ...
- 【转】iOS可执行文件瘦身方法
http://blog.cnbang.net/tech/2544/ 缩减iOS安装包大小是很多中大型APP都要做的事,一般首先会对资源文件下手,压缩图片/音频,去除不必要的资源.这些资源优化做完后,我 ...
- javascript中sleep等待实现
function sleep(milliSeconds) { var startTime = new Date().getTime(); while (new Date().getTime() < ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- java tools: jstack
SYNOPSIS jstack [ option ] pidclick here to see other detail If the given process is running on a 64 ...
- [转]省市二级联动(纯js实现)
转至:http://www.jb51.net/article/41556.htm 实现原理: set_city("省名称",市select对象); 判断市select对象是否为空, ...
- Oracle数据库如何授权收费(Database Licensing)
Oracle软件本身是免费的,所以任何人都可以从Oracle官方网站下载并安装Oracle的数据库软件,收费的是License,即软件授权,如果数据库用于商业用途,就需要购买相应Oracle产品的Li ...
- 修改 SVN 账户密码的方法
记是记不住 的,即便是每天都在用的东西,也有貌似熟悉其实很陌生的时候,或者说根本就是不熟悉.于是需要拿出来经常翻翻,比如我们的SVN账户配置,很简单的一个 case,你可能是svn使用高手,但不一定记 ...
- 小菜学习MVC4-WebApi
今天想看下MVC4的东西,发现 居然有WebApi这东西,百度了一下..居然是 WCF中的东西,然后移植到了MVC4中,WCF你懂得返回数据都是xml,向网站这种请求 就比较纠结...而webapi可 ...