NSDictionary 使用总结
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- //创建字典
- NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
- NSLog(@"dic1 :%@", dic1);
- //创建多个字典
- NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
- @"value1", @"key1",
- @"value2", @"key2",
- @"value3", @"key3",
- @"value4", @"key4",
- nil];
- NSLog(@"dic2 :%@", dic2);
- //根据现有的字典创建字典
- NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
- NSLog(@"dic3 :%@", dic3);
- //根据key获取value
- NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);
- //获取字典数量
- NSLog(@"dic count :%d", dic3.count);
- //所有的键集合
- NSArray *keys = [dic3 allKeys];
- NSLog(@"keys :%@", keys);
- //所有值集合
- NSArray *values = [dic3 allValues];
- NSLog(@"values :%@", values);
- NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"mvalue1", @"mkey1",
- @"mvalue2", @"mkey2", nil];
- //添加现有的字典数据
- [mutableDic addEntriesFromDictionary:dic3];
- NSLog(@"mutableDic :%@",mutableDic);
- //添加新的键值对象
- [mutableDic setValue:@"set1" forKey:@"setKey1"];
- NSLog(@"set value for key :%@",mutableDic);
- //以新的字典数据覆盖旧的字典数据
- [mutableDic setDictionary:dic2];
- NSLog(@" set dictionary :%@",mutableDic);
- //根据key删除value
- [mutableDic removeObjectForKey:@"key1"];
- NSLog(@"removeForkey :%@",mutableDic);
- //快速遍历
- for(id key in mutableDic) {
- NSLog(@"key :%@ value :%@", key, [mutableDic objectForKey:key]);
- }
- //枚举遍历
- NSEnumerator *enumerator = [mutableDic keyEnumerator];
- id key = [enumerator nextObject];
- while (key) {
- NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);
- key = [enumerator nextObject];
- }
- //根据key数组删除元素
- [mutableDic removeObjectsForKeys:keys];
- NSLog(@"removeObjectsForKeys :%@",mutableDic);
- [mutableDic removeAllObjects];
- //删除所有元素
- NSLog(@"remove all :%@", mutableDic);
- }
- return 0;
- }
日志:
NSDictionary 使用总结
- NSArray *m_array = [NSArray arrayWithObjects:@"first",@"second",nil];
- NSArray *n_array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
- //使用类方法初始化,系统自动释放内存
- NSDictionary *test_dictionary = [NSDictionary dictionaryWithObjectsAndKeys:m_array,@"sort",n_array,@"number",nil];
- //获取字典包含对象数目
- int dict_size = [test_dictionary count];
- //访问字典中的值
- NSArray *sort_array = [test_dictionary objectForKey:@"sort"];
- //获取键值
- NSArray *keys = [test_dictionary allKeysForObject:sort_array];
- //获取字典中所有值,数组
- NSArray *all_value = [test_dictionary allValues];
- //快速枚举
- for(id key in test_dictionary)
- {
- NSLog(@"key: %@,value: %@",key,[test_dictionary objectForKey:key]);
- }
- //如果字典只包含属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary)可以保存到文件中
- NSString *filePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"test_dict.plist"];
- [test_dictionary writeToFile:filePath atomically:YES];
- //用文件填充
- NSDictionary *myDict =[NSDictionary dictionaryWithContentsOfFile:filePath];
- //可变字典
- NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc]initWithObjectsAndKeys:m_array,@"sort",n_array,@"number", nil];
- NSString *str = @"10_10";
- //修改对象
- [dictMutable setObject:string4 forKey:@"sort"];
- //删除对象
- [dictMutable removeObjectForKey:@"number"];
- //删除多个对象
- NSArray *key_array =[NSArray arrayWithObjects:@"sort",@"number", nil];
- [dictMutable removeObjectForKey:key_array];
- //删除所有对象
- [dictMutable removeAllObjects];
//k-v 键值对
//key:对象标识符,不重复
//value:对象,可重复
//字典与数组的区别:数组有序,字典无序;
//分 可变字典 不可变字典
//创建三个person对象
Person *person1 = [Person PersonWithName:@"xi" age:18];
Person *person2 = [Person PersonWithName:@"aa" age:9];
Person *person3 = [Person PersonWithName:@"ao" age:3];
//将对象添加到字典中
//
NSDictionary *pDict = [[NSDictionary alloc]initWithObjectsAndKeys:
person1,@"xi",
person2,@"aa",
person3,@"ao", nil];
//便利构造器
NSDictionary *pD = [NSDictionary dictionaryWithObjects:@[person1,person2,person3] forKeys:@[@"xi",@"aa",@"ao"]];
NSLog(@"%@",pDict);
//字面量创建 不可变
NSDictionary *pDict1 = @{ @"xi":person1,
@"aa":person2,
@"ao":person3 };
NSLog(@"%@",pDict);
NSLog(@"%@",pDict);
NSLog(@"-----------------使用枚举器,便利-------------------------");
NSEnumerator *keyEnum = [pDict1 keyEnumerator];
id obj;
while ( obj = [keyEnum nextObject]) {
NSLog(@"key=%@",obj);
}
NSEnumerator *keyEnum1 = [pDict1 objectEnumerator];
id obj1;
while ( obj1 = [keyEnum1 nextObject]) {
NSLog(@"value=%@",obj1);
}
NSLog(@"-----------------使用枚举器,便利-------------------------");
//从字典取出一个对象
Person *p = [pDict objectForKey:@"xi"];
Person *p1 = pDict[@"xi"];
NSLog(@"%@",p);
NSLog(@"%@",p1);
//打印所有key
NSLog(@"key=%@",[pDict allKeys]);
//打印所有value
NSLog(@"value=%@",[pDict allValues]);
//循环的打印字典中的键值对
for (int i = 0; i < pDict.count; i++) {
//先通过循环 从allkeys 数组 取出每一个key
NSString *key = [pDict allKeys][i];
Person *p = pDict[key];
NSLog(@"key=%@,value=%@",key, p);//调用description方法
}
//可变字典
//1,创建
NSMutableDictionary *mutaDic = [NSMutableDictionary dictionary];
NSMutableDictionary *mutaDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:person1,@"ao",person2,@"xi", nil];
//2,添加
[mutaDic1 setObject:person3 forKey:@"aa"];
//3,替换
[mutaDic1 setObject:person3 forKey:@"ao"];
//4,根据key去删除value
[mutaDic1 removeObjectForKey:@"ao"];
//5,删除所有
[mutaDic1 removeAllObjects];
//
//
for (int i = 0; i < mutaDic1.count; i++) {
//先通过循环 从allkeys 数组 取出每一个key
NSString *key = [mutaDic1 allKeys][i];
Person *p = mutaDic1[key];
NSLog(@"key=%@,value=%@",key, p);//调用description方法
}
//1,创建一个数组,
NSArray *classArray = @[@"zhang",@"zhu"];
//2,创建一个数组,
NSArray *classArray1 = @[@"zha",@"zh"];
//1,创建一个大数组,
NSArray *classArrayB = @[classArray,classArray1];
NSDictionary *allClassDict = @{@"36": classArray,
@"37": classArray1,
};
NSLog(@"%@",allClassDict[@"36"][0]);
//NSset 元素唯一,无序,随机抽取,
//1,
NSSet *set = [NSSet setWithObjects:@"c",@"d",@"a", nil];
NSLog(@"%@",set);
NSLog(@"%@",[set anyObject]);
if ([set containsObject:@"c"]) {
NSLog(@"you c");
}
//1,创建 年龄数组
NSArray *ageArray = @[@12, @33, @65];
//使用数组创建集合
NSCountedSet *ageSet = [NSCountedSet setWithArray:ageArray];
//年龄是33的个数
NSLog(@"%lu",[ageSet countForObject:@33 ]);
//快速枚举
NSArray *arr = @[@"wang",@"jin",@"wei",@11];
for (id a in arr) {
NSLog(@"%@",a);
}
NSDictionary *dict = @{@"11": @"v1",
@"22": @"v2"};
//默认取key
for (NSString * key in dict) {
NSLog(@"%@",key);
}
//取value的值
for (NSString * value in [dict allValues]) {
NSLog(@"%@",value);
}
//创建一个保存年龄的数组
NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@"12", @"2", @"9", @"21", nil];
for (int i = 0; i < mArr.count - 1; i++) {
for (int j = 0; j < mArr.count - 1 - i; j++) {
if ([mArr[j] intValue] > [mArr[j+1] intValue]) {
[mArr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSLog(@"%@",mArr);
//默认升序
[mArr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",mArr);
// //创建三个person对象
// Person *person1 = [Person PersonWithName:@"xi" age:18];
// Person *person2 = [Person PersonWithName:@"aa" age:19];
// Person *person3 = [Person PersonWithName:@"ao" age:38];
//
NSMutableArray *personArray = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
NSLog(@"%@",personArray);
//按年龄从小到大排序
for (int i = 0; i < personArray.count - 1; i++) {
for (int j = 0; j < personArray.count - i - 1;j++) {
if ([personArray[j] age] > [personArray[j + 1] age] ) {
[personArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
NSLog(@"%@",personArray);
NSDictionary的常见用法总结
NSArray *array1 = [NSArray arrayWithObjects:@"iphone",@"ipod",nil];
NSArray *array2 = [NSArray arrayWithObjects:@"mac",@"imac",@"mac pro",nil];
//类方法初始化自动释放
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:array1,@"mobile",array2,@"computers",nil];//注意用nil结束
NSLog(@"myDictionary = %@",myDictionary);
int dictSize = [myDictionary count];
//访问字典中的值
NSArray *mobile = [myDictionary objectForKey:@"mobile"];
//从一个对象获取键
NSArray *keys = [myDictionary allKeysForObject:array1];
//获取字典中所有值得一个数组
NSArray *values = [myDictionary allValues];
//快速枚举
for(id key in myDictionary)
{
NSLog(@"key: %@,value: %@",key,[myDictionary objectForKey:key]);
}
//如果字典只包含属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary)可以保存到文件中
NSString *filePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"dict.txt"];
BOOL success = [myDictionary writeToFile:filePath atomically:YES];
//用文件填充
NSDictionary *myDict2 =[NSDictionary dictionaryWithContentsOfFile:filePath];
//可变字典
NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc]initWithObjectsAndKeys:array1,@"mobile",array2,@"computer", nil];
NSString *string4 = @"stringTV";
//修改对象
[dictMutable setObject:string4 forKey:@"media"];
//删除对象
[dictMutable removeObjectForKey:@"mobile"];
//删除多个对象
NSArray *keyArray =[NSArray arrayWithObjects:@"mobile",@"computer", nil];
[dictMutable removeObjectForKey:keyArray];
//删除所有对象
[dictMutable removeAllObjects];
注: iOS 6 新的快捷初始化写法:
NSDictionary:
- NSDictionary *dic = @{@"键":@"值",@"键1":@"值1"};
NSMutableDictionary:
- NSMutableDictionary *MDic = [@{@"键":@"值",@"键1":@"值1"} mutableCopy];
1:基础初始化
- NSMutableDictionary *muDicAsyncImage = [[NSMutableDictionary alloc] init];
2:为字典添加对象(键与值都是 id 接受任何类型)
- [muDicAsyncImage setObject:@"值" forKey:@"键"];
3:通过键取得值对象
- NSString *str= [muDicAsyncImage objectForKey:@"键"];
4:删除某个对象
- [ muDicAsyncImage removeObjectForKey: @"键"];
NSDictionary 使用总结的更多相关文章
- iOS JSON、NSDictionary互转
#import "myCode.h" @implementation myCode /*! * @brief 把格式化的JSON格式的字符串转换成字典 * @param jsonS ...
- 字典NSDictionary以及NSMutableDictionary的用法总结
做过Java语言 或者 C语言 开发的朋友应该很清楚 关键字map 吧,它可以将数据以键值对儿的形式储存起来,取值的时候通过KEY就可以直接拿到对应的值,非常方便.在Objective-C语言中 词典 ...
- iOS常用 --- NSDictionary 与 NSMutableDictionary
一.NSDictionary 字典的两种创建方法 NSDictionary *dic1 =[[NSDictionary alloc]init]; 2 // 或: 3 NSDictionary *dic ...
- Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...
- 一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- 如何删除NSDictionary或NSArray中的NSNull
前段时间与某公司的技术交流,被问到一个问题,如何删除NSDictionary中的NSNull.当时在纸上写,以前太依赖Xcode编译器了,以至于方法名都写不全,最终也没写出来,我想我肯定被鄙视的体无完 ...
- iOS 开发遇到的问题之(nil指针对NSDictionary及NSArray初始化的影响)
nil指针对NSDictionary及NSArray初始化的影响 最近在做项目的时候遇到一个挺坑的崩溃问题,是由于NSDictionary初始化时nil指针引起的崩溃.假设我们现在要初始化一个{key ...
- Objective-C( Foundation框架 一 NSDictionary (NSMutaleDictionary))
NSDictionary 不可变的字典 创建字典的方法 // 创建字典的方式 NSDictionary *dy = [NSDictionary dictionaryWithObject:@" ...
- Foundation ----->NSDictionary
/*_______________不可变字(NSDictionary)____________*/ //1.字典的创建 //值(value) NSArray *arr ...
- 黑马程序员-NSDictionary和NSMutableDictionary
NSDictionary和NSMutableDictionary:通过key和value进行对应,进行存储元素,能够方便提取所需的元素.key是不能够重复出现,但是value能够重复出现.NSDict ...
随机推荐
- hdu2817 A sequence of numbers
这题就是判断是等差数列还是等比数列,然后计算结果mod200907 因为数字比较大10的九次方 所以等比用到了快速幂求模 不懂可以看看算法导论,在大数那里有讲 #include <iostrea ...
- 自己在安装centos 系统时, 是使用英文安装 成功,现在系统语言为英语,如何设置为中文?
作为一个linux菜鸟,遇到的问题可谓真多,在虚拟机VMware上安装好centos系统后,心里甚喜,也连上网络了. 一.遇到的问题 but,火狐浏览器浏览网页出现乱码,也不知道怎么解决?所有的中文都 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...
- OC中类别、扩展、协议与托付
类别(category)--通过使用类别,我们能够动态地为现有的类加入新方法.并且能够将类定义模块化地分不到多个相关文件里.通常仅仅在类别中定义方法. 类别,接口部分的定义,通常该文件命名为已有&qu ...
- JWS-webservice 与Axis2-webservice的高速实现
在详细介绍这两种框架下的webservice之前,先跟大家交流一下SOA认识,也就是面向服务的体系结构.SOA所要解决的主要问题是在现有基础环境的前提下,通过对现有应用程序和基础结构进行又一次的组合以 ...
- 创建文档和自定义的qt assistant
利用qt制作帮助文档 1. 创建文档即是一些html文件,这里可以使用qt提供的工具像是qdoc 和Doxygen生成帮助的html文档. 2. 组织文档结构用于qt ...
- IOS--实现滤镜效果的四种方式
IOS–实现滤镜效果 demo地址: https://github.com/AbeDay/ios–.git 使用CIFilter来完成IOS中滤镜效果 在IOS中可以使用系统自带的方法来达到路径效果: ...
- C# WinForm获取程序所在路径方法
多个获取WinForm程序所在文件夹路径的方法,收藏备忘. 1)获取当前进程的完整路径,包含文件名(进程名). 代码:string str =this.GetType().Assembly.Locat ...
- JAVA 原始国际化例子
import java.text.MessageFormat; import java.util.Locale; import java.util.ResourceBundle; public cla ...
- Android 实现闹钟功能
原文地址:Android 实现闹钟功能作者:Android_Learners 一.手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以 ...