ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:19.60KB ,
资源ID:4916159      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4916159.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(IOS开发之旅IOS常用数据结构NSArrayNSMutableArrayNSDictionaryNSMutableDi.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

IOS开发之旅IOS常用数据结构NSArrayNSMutableArrayNSDictionaryNSMutableDi.docx

1、IOS开发之旅IOS常用数据结构NSArrayNSMutableArrayNSDictionaryNSMutableDiIOS开发之旅-IOS常用数据结构NSArray、NSMutableArray、NSDictionary、NSMutableDi NSArray NSArray基本用法void arrayTest1() /数组初始化最后必须以nil结尾,表示数组元素结束 NSArray *array1 = NSArray allocinitWithObjects:item0,item1,item2,item3,item4,nil; NSLog(%,array1); /*( item1, it

2、em2, item3, item4 )*/ /获取数组元素个数 NSLog(array count : %ld,array1.count); /*array count : 4*/ /获取特定所以元素 NSLog(array1 = %,array11); /* array1 = item1 */ NSLog(array1 = %,array1 objectAtIndex:(NSInteger)1); /* array1 = item1 */ NSLog(firstObject = %,array1.firstObject); /获取第一个元素 /* firstObject = item1 */

3、 NSLog(lastObject = %,array1.lastObject); /获取最后一个元素 /* lastObject = item4 */ /拼接数组 NSLog(%,array1 componentsJoinedByString:,); /* item0,item1,item2,item3,item4 */ /查找元素的索引,如果包含特定元素,返回具体索引,否则返回:9223372036854775807 NSLog(%ld,array1 indexOfObject:item11); /是否包含特定元素 NSLog(%i, array1 containsObject:item1

4、 = TRUE ? 1 : 0); /* 1 */ NSArray *subArray = array1 subarrayWithRange:NSMakeRange(1, 2); NSLog(%,subArray); /* ( item1, item2 ) */ /保存数组元素到本地文件,以xml格式序列化存储 array1 writeToFile:/Users/new/Desktop/array.txt atomically:YES; /* !DOCTYPE plist PUBLIC -/Apple/DTD PLIST 1.0/EN :/ item0 item1 item2 item3 it

5、em4 */ /读取本地文件,反序列化为数组 NSArray *array2 = NSArray allocinitWithContentsOfFile:/Users/new/Desktop/array.txt; NSLog(%,array2); /* ( item0, item1, item2, item3, item4 ) */ /判断数组元素是否相等 BOOL result = array1 isEqualToArray:item0,item1,item2; NSLog(%,result); /* 0 */ NSArray遍历void arrayTest2() NSArray *arra

6、y1 = NSArray allocinitWithObjects:item0,item1,item2,item3,item4,nil; /遍历方法1 for (int index = 0; index array1.count; index+) NSLog(%,array1index); /* item0 item1 item2 item3 item4 */ /遍历方法2 for (NSString *item in array1) NSLog(%,item); /* item0 item1 item2 item3 item4 */ /遍历方法3 /NSEnumerator *enumera

7、tor = array1 reverseObjectEnumerator; /逆向遍历 NSEnumerator *enumerator = array1 objectEnumerator; /正向遍历 NSString *item = nil; while (item = enumerator nextObject) NSLog(%,item); /* item0 item1 item2 item3 item4 */ /遍历方法4 array1 enumerateObjectsUsingBlock:(id obj, NSUInteger idx, BOOL *stop) NSLog(%ld

8、= %,idx,obj); ; /* 0 = item0 1 = item1 2 = item2 3 = item3 4 = item4 */ NSArray排序void arrayTest3() /排序方法1,普通比较器 NSArray *array1 = 1,21,11,5,51,3; NSArray *array2 = array1 sortedArrayUsingSelector:selector(compare:); NSLog(%,array2); /* ( 1, 11, 21, 3, 5, 51 ) */ /排序方法2,代码块 NSArray *array3 = array1 s

9、ortedArrayUsingComparator:NSComparisonResult(id obj1, id obj2) return obj1 compare:obj2; ; NSLog(%,array3); /* ( 1, 11, 21, 3, 5, 51 ) */ /排序方法3,自定义排序描述符 NSArray *originalArray = page_no:27,age:24, page_no:1, age:23, page_no:1, age:21, page_no:1, age:25, page_no:1, age:15, page_no:12,age:19, page_no

10、:23,age:29, page_no:3, age:22, page_no:2, age:30, page_no:17,age:33 ; NSSortDescriptor *alphaNumSD = NSSortDescriptor sortDescriptorWithKey:page_no ascending:YES comparator:NSComparisonResult(NSString *string1, NSString *string2) return string1 compare:string2 options:NSNumericSearch; ; NSSortDescri

11、ptor *dataNumSD = NSSortDescriptor sortDescriptorWithKey:age ascending:YES comparator:NSComparisonResult(id data1, id data2) return data1 compare:data2; ; NSArray *sortedArray = originalArray sortedArrayUsingDescriptors:alphaNumSD,dataNumSD; NSLog(%,sortedArray); /* ( age = 15; page_no = 1; , age =

12、21; page_no = 1; , age = 23; page_no = 1; , age = 25; page_no = 1; , age = 30; page_no = 2; , age = 22; page_no = 3; , age = 19; page_no = 12; , age = 33; page_no = 17; , age = 29; page_no = 23; , age = 24; page_no = 27; ) */ /排序方法4,自定义比较方法 Person *person1 = Person allocinitWithFirstName:hello lastN

13、ame:world age:35; Person *person2 = Person allocinitWithFirstName:abc lastName:def age:25; Person *person3 = Person allocinitWithFirstName:cal lastName:kdl age:22; NSArray *personArray = person1,person2,person3; NSArray *tempArray = personArray sortedArrayUsingSelector:selector(comparePeople:); NSLo

14、g(%,tempArray); /* ( firstName: abc,lastName:def,age:25, firstName: cal,lastName:kdl,age:22, firstName: hello,lastName:world,age:35 ) */ NSMutableArrayNSMutableArray继承NSArray,所以NSArray的所有特性NSMutableArray都有,在此基础上,NSMutableArray主要新增了添加、删除、更新、插入等特性,如下代码演示:void arrrayTest4() NSMutableArray *mutableArray

15、 = NSMutableArray allocinitWithObjects:item0,item1,item2,item3,item4, nil; NSLog(%,mutableArray); /* ( item0, item1, item2, item3, item4 ) */ /添加元素 mutableArray addObject:item5; NSLog(%,mutableArray); /* ( item0, item1, item2, item3, item4, item5 ) */ /插入元素 mutableArray insertObject:inserted item at

16、Index:(NSInteger)1; NSLog(%,mutableArray); /* ( item0, inserted item, item1, item2, item3, item4, item5 ) */ /删除元素 mutableArray removeObject:item5; NSLog(%,mutableArray); /* ( item0, item1, item2, item3, item4 ) */ /更新元素 mutableArray replaceObjectAtIndex:(NSInteger)5 withObject:replacedItem; NSLog(%

17、,mutableArray); /* ( item0, inserted item, item1, item2, item3, replacedItem ) */ /删除所有元素 mutableArray removeAllObjects; NSLog(%ld,mutableArray.count); /* 0 */ NSDictionary 字典初始化void arrayTest5() /字典初始化方式1 NSDictionary *dictionary1 = NSDictionary allocinitWithObjectsAndKeys:value0,key0, value1,key1,

18、 value2,key2, value3,key3, value4,key4, nil; NSLog(%,dictionary1); /* key0 = value0; key1 = value1; key2 = value2; key3 = value3; key4 = value4; */ /字典初始化方式2 NSArray *valueArray = value0,value1,value2,value3,value4; NSArray *keyArray = key0,key1,key2,key3,key4; NSDictionary *dictionary2 = NSDictiona

19、ry allocinitWithObjects:valueArray forKeys:keyArray; NSLog(%,dictionary2); /* key0 = value0; key1 = value1; key2 = value2; key3 = value3; key4 = value4; */ /字典初始化方式3 NSDictionary *dictionary3 = key0:value0, key1:value1, key2:value2, key3:value3, key4:value4 ; NSLog(%,dictionary3); /* key0 = value0;

20、key1 = value1; key2 = value2; key3 = value3; key4 = value4; */ /调用NSDictionary静态方法生成字典 NSDictionary *dictionary4 = NSDictionary dictionaryWithObject:value0 forKey:key0; NSLog(%,dictionary4); /* key0 = value0; */ NSDictionary *dictionary5 = NSDictionary dictionaryWithObjects:valueArray forKeys:keyArr

21、ay; NSLog(%,dictionary5); /* key0 = value0; key1 = value1; key2 = value2; key3 = value3; key4 = value4; */ NSDictionary *dictionary6 = NSDictionary dictionaryWithObjectsAndKeys:value0,key0,value1,key1,value2,key2,value3,key3,value4,key4, nil; NSLog(%,dictionary6); /* key0 = value0; key1 = value1; ke

22、y2 = value2; key3 = value3; key4 = value4; */ 字典基本常用用法void arrayTest6() NSDictionary *dictionary1 = NSDictionary allocinitWithObjectsAndKeys:value0,key0, value1,key1, value2,key2, value3,key3, value4,key4, value4,key5, nil; NSLog(%,dictionary1); /* key0 = value0; key1 = value1; key2 = value2; key3 =

23、 value3; key4 = value4; key5 = value4 */ NSLog(%ld,dictionary1.count); /* 5 */ /输出特定元素 NSLog(%,dictionary1key0); NSLog(%,dictionary1 objectForKey:key0); /* value0 */ /获取所有key NSLog(%,dictionary1.allKeys); /* ( key3, key1, key4, key2, key0 ) */ /获取所有value NSLog(%,dictionary1.allValues); /* ( value3,

24、value1, value4, value2, value0 ) */ /获取特定value对应的所有key NSLog(%,dictionary1 allKeysForObject:value4); /* ( key4, key5 ) */ /分别获取key对应的value,对于不存在的key,返回not found NSLog(%,dictionary1 objectsForKeys:key0,key1,key8 notFoundMarker:not found); /* ( value0, value1, not found ) */ NSDictionary *dictionaryOther = key0:value0; BOOL result = dictionary1 isEqualToDictionary:dictionaryOther; NSLog(%hhd,result); /* 0 */ /保存到本地文件 dictionary1 writeToFile:/Users/new/Desktop/dictionary.txt atomically:YES; /* !DOCTYPE plist PUBLIC -/Apple/DTD PLIST 1.0/EN :/ key0 value0 key1 value1 key2 value2 key3 value3 key4

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1