Objective-C学习-字典类型 NSDictionary 和 NSMutableDictionary
2015-08-04 20:01
621 查看
字典类型 NSDictionary 和 数组NSArray 类型相似,但存在数组里要取值每次都要遍历,这样就浪费了很长的时间,字典的便利就在于他在存取对象的时候,在后面会追加一个 键值,可以理解成一个标志,我们可以根据这个标志很快的找到这个对象,这样就相对于数组的全部遍历就要方便多。
字典类型与数组类型相似,都是存取的对象,不能存取基本的数据类型,如 int , double, char等,类比数组,字典自然也会有两种类型,不可变字典: NSDictionary 和 可变字典 NSMutableDictionary
这两个类型属于自学,因为还没有学到这个地方,如果学到,必将新版本的用法修改过来。
不可变字典 NSDictionary 的大体用法
用法的具体举例
可变字典 NSMutableDictionary
用法的具体举例
字典类型与数组类型相似,都是存取的对象,不能存取基本的数据类型,如 int , double, char等,类比数组,字典自然也会有两种类型,不可变字典: NSDictionary 和 可变字典 NSMutableDictionary
这两个类型属于自学,因为还没有学到这个地方,如果学到,必将新版本的用法修改过来。
不可变字典 NSDictionary 的大体用法
// // main.m // Practise_NSDictionary // // Created by Mac on 15/8/4. // Copyright (c) 2015年 yue. All rights reserved. // #import <Foundation/Foundation.h> #import"Student.h" int main(int argc, const char * argv[]) { /* 不可变字典类型 NSDictionary */ //创建3个学生对象 Student *student1 = [[Student alloc]initWithName:@"小明" Age:19]; Student *student2 = [[Student alloc]initWithName:@"小刚" Age:18]; Student *student3 = [[Student alloc]initWithName:@"小红" Age:16]; /* 初始化一个字典对象NSDictionary,并将上面的3个学生对象存进字典 字典中的数据是以 键值对 的形式出现,并且顺序不能乱,前边是对象,后面是键名 不能丢掉最后的nil */ NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:student1,@"小明",student2,@"小刚",student3,@"小红",nil]; //取出键值是@“小红”的对象 Student *student = [dic objectForKey:@"小红"]; /* 下一句输出的结果是: 2015-08-04 19:40:03.171 Practise_NSDictionary[1351:138915] 这位键值是小红的学生是:小红 */ NSLog(@"这位键值是小红的学生是:%@",[student name]); /* 最新的一种便利,叫做快速创建,更加方便,而且表达也更加清新 ,如下 */ NSDictionary * dic = @{student1:@"小明",student2:@"小刚",student3:@"小红"}; return 0; }
用法的具体举例
// // main.m // NSDictionary(不可变字典) 博客 // // Created by YueWen on 15/9/9. // Copyright (c) 2015年 YueWen. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { /* 不可变字典 */ /*常用的初始化方法*/ //objc方式的创建对象的普遍方法,init是NSObject的方法,几乎所有的类都是继承于NSObject 类 NSDictionary * dict1 = [[NSDictionary alloc] init]; //这种创建对象的方法与上面实质是相同的,只不过上面的是便利初始化函数,下面的是便利构造器来创建,下面就不再注释是便利初始化函数还是便利构造器 NSDictionary * dict2 = [NSDictionary dictionary]; //一种和java 一样的创建对象的方法,刚从java 转过来的程序员会比较青睐与这种初始化 NSDictionary * dict3 = [NSDictionary new]; //创建一个字典,字典的键值对分别是 Key:Value @“name”:@"Yue" @"tele":@"18888888888" NSDictionary * dict4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Yue",@"name",@"18888888888",@"tele",nil]; NSDictionary * dict5 = [NSDictionary dictionaryWithObjectsAndKeys:@"Yue",@"name",@"18888888888",@"tele",nil]; //根据 字典 dict4 来创建字典对象 NSDictionary * dict6 = [[NSDictionary alloc]initWithDictionary:dict4]; NSDictionary * dict7 = [NSDictionary dictionaryWithDictionary:dict4]; //通过数组中存储的信息 来创建字典 NSArray * name = @[@"Yue",@"Wen"]; NSArray * key = @[@"firstName",@"lastName"]; NSDictionary * dict8 = [[NSDictionary alloc]initWithObjects:name forKeys:key]; NSDictionary * dict9 = [NSDictionary dictionaryWithObjects:name forKeys:key]; //通过数据直接定义字典,键值的方式 NSDictionary * dict10 = @{@"firstName":@"Yue",@"lastName":@"Wen",@"first":@"Yue"}; /* 常用的不可变字典的方法 */ /** * 获取字典 dict10 的所有的键(key)值 * 运行结果是:keys = @[@"firstName",@"lastName",@"first"] * 注:这个结果的顺序不是固定的,因为查找的方法为哈希查找,所以数组的顺序有可能不是你所定义的顺序 */ NSArray * keys = [dict10 allKeys]; /** * 获取字典 dict10 的所有的值(value) * 运行结果是:values = @[@"Yue",@"Wen",@"Yue"] * 注:这个结果的顺序也不是固定的,理由同上 */ NSArray * values = [dict10 allValues]; /** * 获取字典 dict10 中的值为@“Yue”的所有的键(key)值 * 运行结果是:valueForKey = @[@"firstName",@“first”] */ NSArray * valueForKey = [dict10 allKeysForObject:@"Yue"]; /** * 比较字典 dict10 和 dict9 是否相同 * 运行结果是:b1 = NO */ BOOL b1 = [dict10 isEqualToDictionary:dict9]; /** * 获取字典 dict10 中键(key)值是@“first”的对象 */ id value = [dict10 objectForKey:@"first"]; return 0; }
可变字典 NSMutableDictionary
// // main.m // Practise_NSMutableDictionary // // Created by Mac on 15/8/4. // Copyright (c) 2015年 yue. All rights reserved. // #import<Foundation/Foundation.h> #import"Student.h" int main(int argc, const char * argv[]) { /* 可变字典类型 NSMutableDictionary */ //创建3个学生对象 Student *student1 = [[Student alloc]initWithName:@"小明" Age:19]; Student *student2 = [[Student alloc]initWithName:@"小刚" Age:18]; Student *student3 = [[Student alloc]initWithName:@"小红" Age:16]; //初始化 //起初创建容量为3的可变字典,这个无所谓,因为可以扩充,与不可变字典不同 NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:3]; //添加对象的方法 [mutableDictionary setObject:student1 forKey:@"小明"]; [mutableDictionary setObject:student2 forKey:@"小刚"]; [mutableDictionary setObject:student3 forKey:@"小红"]; //删除对象的方法 //这句话的作用就是删除 键值是小红 的对象 [mutableDictionary removeObjectForKey:@"小红"]; //这个方法最狠,就是清空了可变的字典里的数据 [mutableDictionary removeAllObjects]; return 0; }
用法的具体举例
// // main.m // NSMutableDictionary (可变字典) 博客 // // Created by YueWen on 15/9/10. // Copyright (c) 2015年 YueWen. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { /* * 可变字典(NSMutableDcitionary) */ /*可变字典的常用初始化方法,很多 不可变字典 的初始化方法也能用在 可变字典 除了直接赋值*/ //objc方式的创建对象的普遍方法,init是NSObject的方法,几乎所有的类都是继承于NSObject 类 NSMutableDictionary * mDict1 = [[NSMutableDictionary alloc]init]; ////这种创建对象的方法与上面实质是相同的,只不过上面的是便利初始化函数,下面的是便利构造器来创建,下面就不再注释是便利初始化函数还是便利构造器 NSMutableDictionary * mDict2 = [NSMutableDictionary dictionary]; //一种和java 一样的创建对象的方法,刚从java 转过来的程序员会比较青睐与这种初始化 NSMutableDictionary * mDict3 = [NSMutableDictionary new]; //根据 字典 mDict1 来创建 字典对象 NSMutableDictionary * mDict4 = [[NSMutableDictionary alloc]initWithDictionary:mDict1]; NSMutableDictionary * mDict5 = [NSMutableDictionary dictionaryWithDictionary:mDict1]; //初始化字典的时候 默认的元素的个数是3个 但是对于可变字典无所谓 因为后面是可以添加的嘛 NSMutableDictionary * mDict6 = [[NSMutableDictionary alloc]initWithCapacity:3]; NSMutableDictionary * mDict7 = [NSMutableDictionary dictionaryWithCapacity:3]; //通过事先放入几个 键值对 对象,来完成初始化,但是一定不要忘记后面你的nil NSMutableDictionary * mDict8 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Yue",@"firstName",@"Wen",@"lastName",@"Run",@"1",nil]; NSMutableDictionary * mDict9 =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Yue",@"firstName",@"Wen",@"lastName",@"Run",@"1",nil]; /** * 可变字典 的常用数据处理方法(比如 allKeys allValues等方法在不可变字典中已经说明,就不再下面说明了) * 一下实例用 原型用mDict9 */ /** * 删除可变字典中 键值(key)为@“1”的对象 * 方法执行前:mDict9 = @{@"fristName":@"Yue",@"lastName":@"Wen",@"1":@"Run"} * 方法执行后:mDict9 = @{@"fristName":@"Yue",@"lastName":@"Wen"} */ [mDict9 removeObjectForKey:@"1"]; /** * 添加一个可变字典的元素 添加一个键值(key) = @"boke" 值(value)= @“RunIntoLove”的键值对 * 方法执行前:mDict9 = @{@"fristName":@"Yue",@"lastName":@"Wen"} * 方法执行后:mDict9 = @{@"boke":@"RunIntoLove",@"fristName":@"Yue",@"lastName":@"Wen"} */ [mDict9 setObject:@"RunIntoLove" forKey:@"boke"]; /** * 删除字典中 所有的键值对 * 方法执行前:mDict9 = @{@"fristName":@"Yue",@"lastName":@"Wen"} * 方法执行后:mDict9 = @{} */ [mDict9 removeAllObjects]; return 0; }
相关文章推荐
- 【Objective-C编程】浅析Objective-C的继承与复合
- Objective-C语法之Objective-C语言和IOS系统(简介,语法,系统结构)
- Objective-C的构造方法
- JSONArray json = JSONArray.fromObject(list_excel); list类型数据转换成JSONArray类型数据报错
- 详解Objective-C的meta-class
- Objective-C多态:动态类型识别+动态绑定+动态加载
- Objective-C响应方法
- Object-C内存管理
- Objective-C 动态类型检测 类和实力对象能否响应方法
- OC基础回顾(八)内存管理
- GObject入门教程
- Objective-C 的动态类型判断
- Objective C 学习笔记
- JSONObject的getString()和optString()的区别
- *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from o
- Objective-C学习- 字符串类型 NSString 和 NSMutableString
- Objective-C学习- 数字对象 NSNumber
- Objective-C学习-数组 NSArray 和 NSMutableArray
- Objective-C学习- 便利初始化函数和便利构造器
- Object-c 典型的单例写法