IOS 开发学习26 NSDictionary的一些操作
2015-05-28 14:54
441 查看
字典
//创建字典 NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
//创建多个值字典 NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", @"value3", @"key3", @"value4", @"key4", nil];
从字典创建 NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
//取值 NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);
//元素数量 NSLog(@"dic count :%d", dic3.count);
//键集合数组 NSArray *keys = [dic3 allKeys];
//值数组 NSArray *values = [dic3 allValues];
可变字典
//可变字典 NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"mvalue1", @"mkey1", @"mvalue2", @"mkey2", nil];
//创建可变字典 设定初始长度 用的时候超出也没关系 [NSMutableDictionary dictionaryWithCapacity:10]
//添加字典 [mutableDic addEntriesFromDictionary:dic3];
//添加键值对 [mutableDic setValue:@"set1" forKey:@"setKey1"];
//添加数据 [dictionary setObject:@"雨松MOMO" forKey:@"name"];
//取值 NSObject *object = [dictionary objectForKey:@"name"];
//字典整体赋值 [mutableDic setDictionary:dic2];
//删除键 [mutableDic removeObjectForKey:@"key1"];
//遍历1 for(id key in mutableDic) { NSLog(@"key :%@ value :%@", key, [mutableDic objectForKey:key]); }
//遍历2 NSEnumerator *enumerator = [mutableDic keyEnumerator]; id key = [enumerator nextObject]; while (key) { NSLog(@"enumerator :%@", [mutableDic objectForKey:key]); key = [enumerator nextObject]; }
//清空 [mutableDic removeAllObjects];
Plist操作
//读取 NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
//写入 [dic writeToFile:testPath atomically:YES];
更多参照这里: http://blog.csdn.net/xundh/article/details/45716015
一个无法新建NSDictionary变量的问题处理:
提示: Thread n:EXC_BAD_ACCESS(code=1,address=0x1)
处理方式 :代码里result1[1]是个byte,ObjectC规定基本数据类型不能直接添加到字典里,可以这样写:
@(result1)
相关文章推荐
- IOS 开发学习25 NSData 的一些基本操作
- iOS开发学习路径的一些建议
- PHPCMS学习:三种开发模式介绍 及 后台一些操作原理
- 关情纸尾-----关于最近学习iOS开发的一些总结(一)
- iOS学习笔记1-开发的一些奇巧淫技
- iOS开发中NSDictionary操作大全
- 分享一些学习iOS的开发心得
- ios开发学习---一些概念的简介
- iOS 开发学习路径的一些建议
- 学习iOS开发的一些网站(不断更新)
- IOS学习笔记68 -- 开发的一些奇巧淫技
- iOS开发学习路径的一些建议
- iOS开发学习路径的一些建议
- 关于学习ios开发的一些笔记
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- iOS开发学习路径的一些建议
- iOS开发学习路径的一些建议
- iOS开发--一些开源的学习资源
- 网友分享的一些常见的学习资料连接地址ios开发