- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Objective-C中NSArray的基本用法示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
NSArray的排序 。
。
NSArray的一些用法 NSArray 只允许装OC对象,并且不能装空值,空代表数组元素的结束 。
#pragma mark - NSArray的基本用法 // 创建一个空数组 NSArray *array = [NSArray array]; // 创建有一个元素的数组 array = [NSArray arrayWithObject:@"123"]; // 创建有多个元素的数组 array = [NSArray arrayWIthObjects:@"a",@"b",nil ];//不能装nil空指针,空值代表数组元素结束 // 将一个数组赋值给一个数组 + (instancetype)arrayWithArray:(NSArray *)array; // 获取元素的个数 int count = [array count]; //和 count = array.count; 相同,都是调用get方法 // 是否包含一个元素 - (bool)containsObject:(id)anObject; // 返回最后一个元素 - (id) lastObject; // 获取index位置的元素 - (id)objectAtIndex:(NSUInteger) index; // 获取元素的位置 - (NSUInteger) indexOfObject:(id)anObject; // 在range范围内查找元素的位置 - (NSUInteger) indexofObject:(id)anObject inRange:(NSRange)range; // 比较两个集合内容是否相同 - (Bool) isEqualToArray:(NSArray *)otherArray; // 返回两个集合中第一个相同的对象元素 - (id) firstObjectCommonWithArray:(NSArray *)otherArray,
。
#pragma mark - NSArray的高级用法 //让集合里面的所有元素都执行aSelector这个方法 - (void)makeObjectsPerformSelector:(SEL)aSelector; //让集合里面的所有元素都执行aSelector这个方法,给这个方法添加参数,但是只支持一个参数 - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument //添加一个元素,返回一个新的NSArray(方法调用者本身没有发生变化) - (NSArray *)arrayByAddingObject:(id)anObject //添加otherArray的所有元素,返回一个新的NSArray(方法着本身没有改变) - (NSArray *) arrayByAddingObjectsFromArray:(NSArray *) otherArray; //截取range范围的数组 - (NSArray *) subarrayWithRange:(NSRenge)range; //用separator做拼接符,拼接成一个字符串 - (NSString *) componentsJoinedByString:(NSString *)separator //将NSArray持久化到文件中去 - (Bool) writeToFile:(NSString *)path atomically:(Bool)useAuxiliaryFile 。
#pragma mark - NSArray的遍历 // 方法一:普通遍历(利用for循环) void arrayFor1(){ NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; int count = array.count; for(int i=0; i<count; i++){ id obj = [array objectAtIndex:i]; NSLog(@"%i-%@",i, obj); } } 。
// 方法二:快速遍历 void arrayFor2(){ NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; int count = array.count; int i=0; for(id obj in array){ NSLog(@"%i-%@",i, obj); i++; } } 。
// 方法三:利用block遍历 void arrayFor3(){ NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%zi->%@",idx, obj); // *stop = YES; //改变外边的Bool,终止遍历 }]; } 。
// 方法四:利用迭代器 先介绍一下-->NSEnumerator迭代器:集合的迭代器,可以用于遍历集合元素,NSArray 有相应的方法来获取迭代器 //获取一个正序遍历的迭代器 - (NSEnumerator *) objectEnumerator; //获取一个反序遍历的迭代器 - (NSEnumerator *) reverseObjectEnumerator; @常用方法: //获取下一个元素 - (id) nextObject; //获取所有的元素 - (NSArray *) allObjects void arrayFor4(){ NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; NSEnumerator *enumerator = [array objectEnumerator];// 返回数组的迭代器 //如果放到遍历之后,则取到空,原因是,遍历完了,就没值了 NSArray *array2 = [enumerator allObjects]; NSLog(@"array2=%@", array2); //获取下一个需要遍历的元素 id obj = nil; while (obj = [enumerator nextObject]) { NSLog(@"obj=%@", obj); } } 。
。
使用block 块遍历整个数组。这个block 需要三个参数,id obj 表示数组中的元素。 NSUInteger idx 标示元素的下标, boolbool *stop 是一个bool类型的参数。 官方描述如下: A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block. - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx,BOOLBOOL *stop))block 调用例子如: 。
同上面的方法一项,区别在于,这里多添加了一个参数,用来标示 是从前向后遍历,还是从后往前遍历。 - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx,BOOLBOOL *stop))block 调用例子如下: 。
最后此篇关于Objective-C中NSArray的基本用法示例的文章就讲到这里了,如果你想了解更多关于Objective-C中NSArray的基本用法示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
最近我在用 RestSharp消耗我的 Restful 资源。并期望在服务器和客户端之间与 JSon 交换数据。下面是我的 C# 代码。 var client = new RestSharp.Rest
我正在阅读 Bartosz Milewski 的一篇文章,其中他定义了以下函数: instance Applicative Chan where pure x = Chan (repeat x)
‘…' 其实是go的一种语法糖。 它的第一个用法主要是用于函数有多个不定参数的情况,可以接受多个不确定数量的参数。 第二个用法是slice可以被打散进行传递。 实例:
前言 在算face_track_id map有感: 开始验证 data={"state":[1,1,2,2,1,2,2,2],"pop":[&quo
本文实例讲述了php访问数组最后一个元素的函数end()用法。分享给大家供大家参考。具体分析如下: end()函数在PHP中用于检索数组中的最后一个元素。end()函数需要一个数组作为其唯一参数,
我使用的是 jdk1.8.0_92。我的虚拟机如下所示。 $java -version java version "1.8.0_92" Java(TM) SE Runtime Environment
我的情况是我需要将所有匹配 http://mywebsite.com/portfolio/[anyname] 的请求定向到 http://mywebsite.com/portfolio.php?用户名
我正在尝试在 NLTK 中使用语音标记并使用了以下命令: >>> text = nltk.word_tokenize("And now for something completely differe
#include typedef QList IntList; qRegisterMetaType("IntList"); error C2909: 'qRegisterMetaType':
来自 here我知道 BN_CTX 是一个保存 BIGNUM 临时变量的结构。这些 BIGNUM 变量什么时候会进入 BN_CTX 的 BN_POOL?如果我有一个 bignum_ctx BN_CTX
尝试为 ABPersonRef 创建对象例子:ABpersonRef 引用; 已包含Addressbook和AddressBookUI框架即使这样,当我编译时,它仍显示“ABPersonRef”未声明
我无法使用 GetAltTabInfo。可能是一个愚蠢的错误,但这有什么问题呢? HWND taskSwitcher = FindWindow(L"TaskSwitcherWnd", L"Task S
JSLint4Java 是 JSLint 的 Java 包装器。我需要这样的东西在我的 GWT 项目中使用,但使用 JSLint4Java 的唯一方法似乎是从命令行或通过 ANT 任务。有谁知道是否有
我有一个持久化实体对象的方法 persistData() 。我有另一个方法 findData() ,它对同一实体类执行 find() 操作以获取持久的主键值。当我在实体类的@PostPersist中调
下面是我的代码。请查看。 1. bool isUnavailable = db.Deploys.Where(p => p.HostEnvironmentId == Guid.Parse(h
这个问题已经有答案了: Why can't a Generic Type Parameter have a lower bound in Java? (6 个回答) 已关闭 9 年前。 我试图理解为什
我正在尝试使用 scala 编译器 Y 警告,但我认为我做得不对。在下面的示例中,nums 未使用,因此我希望 -Ywarn-value-discard 打印一个警告。有两个 if 条件,一个嵌套在另
用户被要求从某个给定的集合中选择一个 ID。我检查该 ID 是否存在于我的集合中,如果不存在,我会抛出 IndexOutOfBoundsException 并稍后捕获它。我实际上可以使用该异常来达到这
我正在尝试减少从 OSM 路径数据生成的形状文件。我正在使用 VTS 的 DouglasPeuckerSimplifier 实现。我想为特定 GTFS(通用交通提要规范)构建路线图的 geojson。
我明白了?!是排除某个模式,例如 a(?!b) 表示如果“a”后面没有“b”,它将匹配“a”。我的问题是,假设我有一个包含以下内容的文件: a cat is a cat, a dog is a dog
我是一名优秀的程序员,十分优秀!