容器类的常用操作(NSArray,NSNumber,NSDictionary,NSSet)
1.数组 : 对象,可为任意类型
//(*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查valueNSString *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"];
容器类的常用操作(NSArray,NSNumber,NSDictionary,NSSet)相关推荐
- NSString NSArray NSDictionary NSSet 中的部分方法
NSString NSArray NSDictionary NSSet 中的部分方法 //1.init // NSString *s1 = [[NSString alloc]init] ...
- IOS沙盒Files目录说明和常用操作
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...
- 关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary
NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎 ...
- UITableView取消选中颜色、常用操作
UITableView取消选中颜色.常用操作 使用空白view取代cell - (UITableViewCell *)tableView:(UITableView *)tableView cellFo ...
- (类簇)NSString,NSArray,NSNumber等类的继承问题
问题引入,我想给NSString类扩展一些新的方法.在Objective-C中可以有两种方法,一是继承,二是类别. 本文先不讨论类别,我们用继承的方法试一下: [c] view plain copy ...
- TensorFlow常用操作:代码示例
1,定义矩阵代码示例: import tensorflow as tftf.zeros([3,4]) #定义3行4列元素均为0的矩阵tensor=tf.constant([1,2,3,4])#定义一维 ...
- Ubuntu 常用操作
Ubuntu常用操作 外观操作 修改应用icon图标 终端美化 内存管理 内存查询 分区管理 日志清理 操作执行 自动执行多条命令 外观操作 修改应用icon图标 在/usr/share/applic ...
- 用Python轻松搞定Excel中的20个常用操作
来源 |早起Python(ID: zaoqi-python) Excel与Python都是数据分析中常用的工具,本文将使用动态图(Excel)+代码(Python)的方式来演示这两种工具是如何实现数据 ...
- python和R对dataframe的常用操作的差异:head、tail、shape、dim、nrow、ncol、descirbe、summary、str
python和R对dataframe的常用操作的差异:head.tail.shape.dim.nrow.ncol.descirbe.summary.str # python df.head() # R ...
最新文章
- MEMS传感器科普文
- 以太坊私链搭建、truffle项目开发
- EXT.NET GridPanel展开与收缩
- Java8之Consumer、Supplier、Predicate和Function攻略
- 【网址收藏】WIN10进入超级管理员账号(Administrator)
- 《Effective C#》Item 21:用委托实现回调
- Diffie-Hellman:安全网络通信背后的天才算法
- PS教程第十一课:会打开 会关闭我回了
- 数据结构探险——树篇
- WebStorm 9 配置 Live Edit 功能与浏览器实现同步
- 我的世界java村民繁殖_我的世界:1.14版本刷新几率小的五种村庄,没有村民咋回事?...
- JAVA中修改顺序表中的元素_在Java中修改列表的每个项目
- 【福利派送】高质量AI书籍又免费派送啦
- c语言控制单片机38译码器,单片机+38译码器控制的数码管怎么点亮?
- 【基础】创建react脚手架
- WordPress使用腾讯云CDN配置如何实现https访问?
- 量子多体理论怎么样理解,多体系统的量子理论
- MIMO技术中的各种增益分析
- python 获取屏幕颜色_Python3 获取手机屏幕指定坐标点的 RGBA 和 16 进制颜色
- mysql -省市区(市)