容器类的常用操作(NSArray,NSNumber,NSDictionary,NSSet)
2014-11-03 20:48
387 查看
1.数组 : 对象,可为任意类型
2.NSNumber(数值类)的常用方法
3.NSDictionary :
//NSDictionary的常用方法
//保存键值对(key - value对)的容器
//将一个键值对看作一个元素(实体)。
//靠 key
存储元素
NSSet集合 :
无序性(eg:整数集,
自然数集)
//(*1.)NSArray的常用方法 NSString *str1 = [[NSString alloc]initWithFormat:@"%d杨毅",0]; NSString *str2 = [[NSString alloc]initWithFormat:@"%d陈昕",1]; NSString *str3 = [[NSString alloc]initWithFormat:@"%d安国伟",2]; //1.创建数组对象 NSArray *array1 = [[NSArray alloc]initWithObjects: str1, str2, str3, nil];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="font-family: Menlo;"><span style="white-space:pre"> </span>/// </span>将数组生成新的字符串<span style="font-family: Menlo;">,</span>以<span style="font-family: Menlo;">"-"</span>隔开</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSString</span> *q = [array1 <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">componentsJoinedByString</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"-"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="font-family: Menlo;"><span style="white-space:pre"> </span>/// </span>取一部分返回新数组</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSArray</span> * arrA = [array1 <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">subarrayWithRange</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSMakeRange</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">1</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">2</span>)];</p><div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="font-family: Menlo;">//2.</span>获取数组中的对象</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSString</span> *obj = array1[<span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">2</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%@"</span>,obj);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%@"</span>,array1[<span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">0</span>]);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%@"</span>, [array1 <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">objectAtIndex</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">1</span>]); <span style="font-variant-ligatures: no-common-ligatures; color: #1d9421">// best</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="font-family: Menlo;">//3.</span>获取数组的长度<span style="font-family: Menlo;">(</span>元素个数<span style="font-family: Menlo;">)</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%ld"</span>, [array1 <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">count</span>]);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; min-height: 21px;">//4.<span style="font-family: 'Heiti SC Light';">查询某个对象的下标</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSArray</span> *array2 = [<span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSArray</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">arrayWithObjects</span>:</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>@"<span style="font-family: 'Heiti SC Light';">赵生辉</span>:http://blog.csdn.net/zhaoshenghui11"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>@"<span style="font-family: 'Heiti SC Light';">张世琦</span>: http://blog.csdn.net/zhangshiqi11"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>@"<span style="font-family: 'Heiti SC Light';">靳姜珊</span>: http://blog.csdn.net/hmpcx"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>@"<span style="font-family: 'Heiti SC Light';">张宁浩</span>: http://blog.csdn.net/zxcznh2011/article/details/38472461"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">,</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>@"<span style="font-family: 'Heiti SC Light';">宋旭东</span>: http://blog.csdn.net/belanote"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #c32275">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span>@"%lu"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">, [array2 </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">indexOfObject</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span>@"<span style="font-family: 'Heiti SC Light';">靳姜珊</span>: http://blog.csdn.net/hmpcx"<span style="font-variant-ligatures: no-common-ligatures; color: #000000">]);</span></p></div>
//(*2.)NSMutableArray 常用方法 //1.创建数组对象。 NSMutableArray *array = [NSMutableArray arrayWithObjects: @"周琦", @"安国伟", @"常宽", @"郑海坤", @"王晨", @"马英", @"符之飞", nil]; //2.添加元素 [array addObject:@"张宁浩"]; //3.插入元素 [array insertObject:@"杨毅" atIndex:1];//插到了下标为1的位置 //4.删除元素 [array removeObject:@"安国伟"]; [array removeObjectAtIndex:5];//按照数组下标位置删除 [array addObject:@"周琦"]; [array removeObject:@"周琦" inRange:NSMakeRange(0, [array count] / 2)]; //5.替换元素 [array replaceObjectAtIndex:0 withObject:@"陈昕"]; //6.交换指定位置的两个元素 [array exchangeObjectAtIndex:2 withObjectAtIndex:4]; for (int i = 0; i < [array count]; i++) { NSLog(@"%d %@", i, [array objectAtIndex:i]); } //7.OC-容器类,快速遍历 //不能把基本数据类型,放入NSArray内 for (NSString *str in array) { NSLog(@"%@",str); }
2.NSNumber(数值类)的常用方法
//1.基本数据类型封装为对象 NSNumber *n1 = [NSNumber numberWithInt:12]; NSNumber *n2 = [NSNumber numberWithInt:5]; NSNumber *n3 = [NSNumber numberWithInt:11]; NSNumber *n4 = [NSNumber numberWithInt:6]; NSNumber *n5 = [NSNumber numberWithInt:7]; NSMutableArray *arrayNum = [NSMutableArray arrayWithObjects: n1, n2, n3, n4, n5, nil];
//2.系统帮我们写好了一种排序方式 // 可变数组的排序方法 [arrayNum sortUsingSelector:@selector(compare:)]; // 不可变数组的排序方式 NSArray *newArray = [arrayNum sortedArrayUsingSelector:@selector(compare:)];
3.NSDictionary :
//NSDictionary的常用方法
//保存键值对(key - value对)的容器
//将一个键值对看作一个元素(实体)。
//靠 key
存储元素
//1.创建字典对象 //输入格式: 值在左,键在右 NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys: @"ZhangSanFeng", @"name", @"male", @"sex", @"female", @"hobby", @"347", @"age", nil]; //字典的无序性 //输出格式:键在左,值在右 NSLog(@"%@", dic1); //1.获取所有key值 NSArray *keys = [dic1 allKeys]; NSLog(@"%@", keys); //2.获取所有value值 NSArray *values = [dic1 allValues]; NSLog(@"%@", values); //3.根据key查value NSString *name = [dic1 objectForKey:@"name"]; //4.间接遍历字典 //根据allkeys数组获取字典中所有的key-value对 for (NSString *key in keys) {//keys 是获取 dic1 中所有的键key对 NSString *value = [dic1 objectForKey:key]; NSLog(@"%@ = %@;", key, value); } //5.直接遍历字典 for (NSString *key in dic1) { NSString *value = [dic1 objectForKey:key]; NSLog(@"%@ = %@;", key, value); }
//NSMutableDictionary 的常用方法 //1.创建 NSMutableDictionary *dicM = [NSMutableDictionary dictionaryWithDictionary:dic1]; //2.添加键值对 [dicM setValue:@"WuDang" forKey:@"attribute"]; //3.修改key对应的value [dicM setValue:@"Shua Jian" forKey:@"hobby"]; //4.删除键值对 [dicM removeObjectForKey:@"sex"];
NSSet集合 :
无序性(eg:整数集,
自然数集)
//1. 创建集合对象 NSSet *set = [NSSet setWithArray:newArray]; NSLog(@"%@", set); //2. 获取元素个数 NSLog(@"%ld", [set count]); //3. 获取集合中的某个元素 NSLog(@"%@", [set anyObject]); //4. 判断结合中是否包含某个对象 NSSet *setString = [NSSet setWithObjects:@"1", @"2", @"3", @"4", nil]; if ([setString containsObject:@"3"]) { NSLog(@"包含"); } //NSMutableSet的常用写法 NSMutableSet *setM = [NSMutableSet setWithSet:set]; //1.添加元素 [setM addObject:@"7"]; //2.删除元素 [setM removeObject:@"2"];
相关文章推荐
- 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber
- IOS开发NSArray,NSSet,NSDictionary,NSString操作总结
- Objective C - Foundation - NSNumber & NSString & NSArray & NSDictionary & NSSet
- NSArray 与 NSSet 与NSDictionary 常用的用法
- 随笔-常见的数据类型常用操作整理-NSString/NSArray/NSDictionary/NSData
- 黑马程序员—— Foundation框架NSArray、NSSet、NSDictionary、NSNumber、NSDate、NSValue
- OC第五讲(NSNumber,NSArray,NSDictionary,NSSet等)
- 黑马程序员--NSArray,NSString,NSSet,NSDictionary,NSNumber,NSValue 及其可变(NSMutable..)
- NSArray,NSSet,NSDictionary 操作函数
- NSArray、 NSSet、 NSDictionary 区别:
- NSArray,NSSet,NSDictionary总结
- NSArray,NSSet,NSDictionary总结
- 用法总结:NSArray,NSSet,NSDictionary
- NSArray,NSSet,NSDictionary总结
- NSArray NSSet NSDictionary
- NSArray,NSDictionary, NSSet 的差异
- NSArray,NSSet,NSDictionary
- [iphone 开发]NSArray,NSSet,NSDictionary总结
- NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍
- NSDictionary,NSArray,NSSet