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 ...
随机推荐
- 链表逆序(JAVA实现)
题目:将一个有链表头的单向单链表逆序 分析: 链表为空或只有一个元素直接返回: 设置两个前后相邻的指针p,q,使得p指向的节点为q指向的节点的后继: 重复步骤2,直到q为空: 调整链表头和链表尾: 图 ...
- HDU 5623 KK's Number (博弈DP)
KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...
- ASP.NET WebForm中前台代码如何绑定后台变量
转载自 http://www.cnblogs.com/lerit/archive/2010/10/22/1858007.html 经常会碰到在前台代码中要使用(或绑定)后台代码中变量值的问题.一般有& ...
- MSSQL索引优化
转自:http://blog.itpub.net/16436858/viewspace-589275/ http://www.cnblogs.com/jams742003/archive/2011/1 ...
- Oracle-11g 从表空间删除数据文件
从表空间删除数据文件前提条件 如果欲从表空间中删除数据文件,那么该数据文件必须为空,否则将报出"ORA-03262: the file is non-empty"的错误. 从表 ...
- python的random模块
As an example of subclassing, the random module provides the WichmannHill class that implements an a ...
- Android中图表AChartEngine学习使用与例子
很多时候项目中我们需要对一些统计数据进行绘制表格,更多直观查看报表分析结果.基本有以下几种方法: 1:可以进行android api进行draw这样的话,效率比较低 2:使用开源绘表引擎,这样效率比 ...
- 处理Oracle中杀不掉的锁
一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...
- 【M3】绝对不要以多态方式处理数组
1.考虑下面的情况,有个方法,如下: void Print(ostream& s, const Base array[], int size) { for(int i=0; i< siz ...
- 从零开始学习Hadoop--第2章 第一个MapReduce程序
1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...