黑马程序员-Object C之NSDirectory和NSMutableDirectory的基本使用
来源:互联网 发布:论大数据的机遇和挑战 编辑:程序博客网 时间:2024/06/16 03:29
------------------------------ASP.Net+Unity开发、.Net培训、期待与您交流!---------------------------
NSDictionary 不可变的字典
例子代码解析:
#pragma mark 字典的初始化void dictCreate() { // NSDictionary是不可变的 NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"]; // 最常用的初始化方式,结尾必须使用nil标志结束 dict = [NSDictionary dictionaryWithObjectsAndKeys: @"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil]; NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil]; NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];//使用NSArray的值初始化字典 NSLog(@"%@", dict);}#pragma mark 字典的基本用法void dictUse() { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil]; // count是计算有多少个键值对(key-value) NSLog(@"count=%zi", dict.count); // 由于NSDictionary是不可变的,所以只能取值,而不能修改值 id obj = [dict objectForKey:@"k2"]; NSLog(@"obj=%@", obj); // 将字典写入文件中 NSString *path = @"/Users/wenli/Desktop/dict.xml"; [dict writeToFile:path atomically:YES]; // 从文件中读取内容 dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"dict=%@", dict);}#pragma mark 字典的用法void dictUse2() { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil]; // 返回所有的key NSArray *keys = [dict allKeys]; //NSLog(@"keys=%@", keys); NSArray *objects = [dict allValues]; //NSLog(@"objects=%@", objects); // 根据多个key取出对应的多个value // 当key找不到对应的value时,用marker参数值代替 objects = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"]; NSLog(@"objects=%@", objects);}#pragma mark 遍历字典void dictFor() { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil]; // 遍历字典的所有key for (id key in dict) { id value = [dict objectForKey:key]; NSLog(@"%@=%@", key, value); }}#pragma mark 遍历字典2void dictFor2() { 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); } // 对象迭代器 // [dict objectEnumerator];}#pragma mark 遍历字典3void dictFor3() { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil]; [dict enumerateKeysAndObjectsUsingBlock://该方法<span style="color: rgb(51, 51, 51); font-family: 宋体;font-size:10px; line-height: 21px;">会遍历dictionary并把里面所有的key和value一组一组的展示</span> ^(id key, id obj, BOOL *stop) { NSLog(@"%@=%@", key, obj); }];}#pragma mark void dictMemory() { Student *stu1 = [Student studentWithName:@"stu1"]; Student *stu2 = [Student studentWithName:@"stu2"]; Student *stu3 = [Student studentWithName:@"stu3"]; // 一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: stu1, @"k1", stu2, @"k2", stu3, @"k3", nil]; // 当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1}
NSMutableDirectory 不可变的字典
例子代码演示:
// 创建一个空的字典 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; Student *stu1 = [Student studentWithName:@"stu1"]; // 添加元素 // stu1的计数器会+1 [dict setObject:stu1 forKey:@"k1"]; NSLog(@"stu1:%zi", [stu1 retainCount]); // 添加其他字典other到当前字典dict中 NSDictionary *other = [NSDictionary dictionaryWithObject:@"v2" forKey:@"k2"]; [dict addEntriesFromDictionary:other]; // 删除所有的键值对 // [dict removeAllObjects]; // 删除k1对应的元素stu1,stu1会做一次release操作 [dict removeObjectForKey:@"k1"]; NSLog(@"stu1:%zi", [stu1 retainCount]); // 删除多个key对应的value // [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]]; // 字典被销毁时,内部的所有key和value计数器都会-1,也就是说stu1会release一次
-----------------------------ASP.Net+Unity开发、.Net培训、期待与您交流!-------------------------
详细请查看:www.itheima.com
0 0
- 黑马程序员-Object C之NSDirectory和NSMutableDirectory的基本使用
- 黑马程序员-Object C之copy和mutableCopy语法(二)
- 黑马程序员-Object C之单个对象和多个对象的内存管理
- 黑马程序员-Object C之set方法的内存管理
- 黑马程序员-Object C之分类
- 黑马程序员-Object-C之构造方法
- 黑马程序员-Object C之block 块
- 黑马程序员-Object C之循环引用
- 黑马程序员-Object C之property
- 黑马程序员-Object C之NSArray
- 黑马程序员----Objective-C学习笔记之autorelease基本使用
- 黑马程序员-第1天 Xcode的基本使用和C语言的重点
- 黑马程序员--Object-C 对象的特性
- 黑马程序员------object-c简史及对比基本语法与其他语言的不同
- IOS前奏之Object-c @property和@synthesize的基本使用
- 黑马程序员-C语言函数的基本定义和注意事项
- 黑马程序员—C语言笔记—了解Mac的基本使用和C语言基础语法
- 黑马程序员--Object-C 类别
- hdu1002A + B Problem II
- CF #244 (Div. 2) D Match & Catch (后缀数组 仅出现一次最短公共子串)
- QBC
- C_PlusPlus学习笔记 - 3_类与对象 (C++语言程序设计【第三版】 郑莉等,清华大学出版社)
- JS中对数字四舍五入
- 黑马程序员-Object C之NSDirectory和NSMutableDirectory的基本使用
- 西北工业大学2010年算法机试分水果
- 随页面滚动而滚动的工具条
- windows下面建立VPN连接,无需安装客户端
- 青润老师
- 第一次来
- MapReduce
- tcp与udp的区别
- JSP Session cookie 详解