【语法】NSDictionary数据字典
2014-01-31 23:39
92 查看
参照练习nsdictionary
【1】字典的创建
2.
3.
【2】字典的使用
1.
2.
3.将字典写入文件中
4.
【3】字典的用法2
2.
3.
【4】遍历字典
2.迭代器
3.这里只提供方法
4.
我们不能在数组中放入nil值,因为NSArray和NSDicSonary中nil有特殊意义。 使用NSNull可以消除这种歧义。
【1】字典的创建
//静态方法,不需要释放内存 NSDictionary *dit=[[NSDictionary alloc] init]; NSDictionary *dict=[NSDictionary dictionaryWithObject:@"v" forKey:@"k"]; NSLog(@"%@",dict);
2.
//最常用的初始化方式 dict=[NSDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2",nil]; NSLog(@"%@",dict);
3.
NSArray *obkects=[NSArray arrayWithObjects:@"v1",@"v2",@"v3",nil]; NSArray *keys=[NSArray arrayWithObjects:@"k1",@"k2",@"k3", nil]; dict=[NSDictionary dictionaryWithObjects:obkects forKeys:keys]; NSLog(@"%@",dict);
【2】字典的使用
1.
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; //count是计算有多少个键值对; NSLog(@"count=%zi",[dict count]);
2.
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; //这个是来查看键值的另外一个 //由于NSdictionary是不可变的,所以只能取值,不能修改值 id obj=[dict objectForKey:@"k2"]; NSLog(@"obj=%@",obj);
3.将字典写入文件中
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; NSString *path=@"/Users/haiyefeng/Desktop/test.txt"; [dict writeToFile:path atomically:YES];
4.
//读取文件中的字典。 NSString *path2=[NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"%@",path2);
【3】字典的用法2
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; NSArray *key= [dict allKeys]; NSLog(@"keys=%@",key);
2.
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; NSArray *objects=[dict allValues]; NSLog(@"%@",objects);
3.
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; objects= [dict objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k555",nil] notFoundMarker:@"not found"];//notfoundmarker里如果找不到的话,当key找不到对应的value时,用maker参数值代替 NSLog(@"objects=%@",objects);
【4】遍历字典
void dictFot(){ NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; //遍历字典的所有key for(id key in dict){ id value =[dict objectForKey:key]; NSLog(@"%@",value); } }
2.迭代器
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; //key 迭代器 NSEnumerator *enumer=[dict keyEnumerator]; id key =nil; while (key=[enumer nextObject]){ id value =[dict objectForKey:key]; NSLog(@"%@=%@",key,value); }
3.这里只提供方法
//对象迭代器 //[dict objectEnumerator];
4.
//遍历字典3 void dictFor3(){ NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: @"v1",@"k1", @"v2",@"k2", @"v3",@"k3",nil]; [dict enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) { NSLog(@"%@=%@",key,obj); }]; }
我们不能在数组中放入nil值,因为NSArray和NSDicSonary中nil有特殊意义。 使用NSNull可以消除这种歧义。
NSMutableArray *array=[NSMutableArray array]; NSMutableDictionary *dict=[NSMutableDictionary dictionary]; NSNumber *number=[NSNumber numberWithInt:100]; [array addObject:number]; [dict setObject:number forKey:@"number"]; NSRect rect=NSMakeRect(1, 2, 30, 40); NSValue *value=[NSValue valueWithBytes:&rect objCType:@encode(NSRect)]; [array addObject:value]; [dict setObject:value forKey:@"rect"]; [array addObject:[NSNull null]]; [dict setObject:[NSNull null] forKey:@"null"]; //输出 for (NSObject *item in array ) { NSLog(@"%@",item); } NSLog(@"=========================="); for (NSString *key in dict) { NSLog(@"%@=%@",key,[dict objectForKey:key]); }
相关文章推荐
- [示例]NSDictionary-字典中的数据转到数组,排序后输出
- Python3组合数据类型(元组、列表、集合、字典)语法
- Swift入门基础(语法,数据类型,数组,字典)
- iOS开发之将字典NSDictionary转化为NSData数据
- [示例]NSDictionary-存数据到字典及@(xxx)强转为对象的用法
- objective-c 中数据类型之四 字典(NSDictionary)
- Objective-C语法-字典NSDictionary和可变字典NSMutableDictionary
- 数据字典生成工具之旅(6):NVelocity语法介绍及实例
- 生成数据字典语法
- objective-c 中数据类型之四 字典(NSDictionary)
- 字典数据存入数据库(转换为二进制)
- 用户如何有效地利用数据字典
- 黑马程序员_iOS开发之OC之面向对象之id语法、构造方法、@category分类、类本质、description方法和SEL数据类型
- 给大家说明。升级数据字典,解决ORA-01092: ORACLE instance terminated. Disconnection forced问题
- ORACLE常用的SQL语法和数据对象
- python使用点操作符访问字典(dict)数据的方法
- 数据绑定表达式语法Eval和Bind
- ztree:数据字典树,右键菜单
- .net 将字典数据转换成xml 字符串, 以及传参至存储过程后如何取值
- 用“DB查询分析器”的对象浏览器来展现数据库的数据字典