枚举数组
来源:互联网 发布:学日语的软件 编辑:程序博客网 时间:2024/06/05 23:59
objectAtIndex: enumeration
使用一个 for 循环,递增循环变量,然后用 [myArray objectAtIndex:index] 来访问元素。这是最基本的枚举形式。
1234NSUInteger count = [myArray count];
for
(NSUInteger index = 0; index < count ; index++) {
[self doSomethingWith:[myArray objectAtIndex:index]];
}
NSEnumerator
外部迭代(external iteration)的形式: [myArray objectEnumerator] 返回一个对象,这个对象有 nextObject 方法。我们可以循环调用这个方法,直到返回 nil 为止。
12345NSEnumerator *enumerator = [myArray objectEnumerator];
id object;
while
(object = [enumerator nextObject]) {
[self doSomethingWith:object];
}
NSFastEnumerator
The idea behind 快速枚举 的思想是利用 C 数组快速访问 来优化迭代。不仅它理论上比传统的 NSEnumerator 更快,而且 Objective-C 2.0 提供了这种简明的语法:
1234id object;
for
(object in myArray) {
[self doSomethingWith:object];
}
Block enumeration(块枚举)
引入 blocks 后出现的方法,它可以基于块来迭代访问一个数组。它的语法没有快速枚举那么简洁,但它有一个有趣的特性: 并发枚举。如果枚举的顺序并不重要,而且实施的处理可以并发进行,不用锁,这种方法可以在多核系统上带来相当明显的效率提升。详情参考 并发枚举一节。
123456[myArray enumerateObjectsUsingBlock:^(id object, NSUInteger index,
BOOL
*stop) {
[self doSomethingWith:object];
}];
[myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx,
BOOL
*stop) {
[self doSomethingWith:object];
}];
0 0
- 枚举数组
- 枚举数组
- php 关联数组?枚举数组?
- 枚举数组接口
- C#枚举数组
- 数组与枚举
- c++数据类型--枚举、数组
- 枚举及数组
- 数组,结构,枚举
- 枚举 结构 数组
- 关于 PHP 枚举数组
- 【基础知识】数组和枚举
- C#数组 枚举 结构
- 数组、结构和枚举
- VHDL 枚举与数组
- 数组的枚举
- 数组枚举部分
- 结构、枚举、数组
- Android之ActionBar、Tabs、Fragment、ViewPager实现标签页切换并缓存页面
- 文章标题
- Android代码优化技术
- android数据库操作之cursor
- csu 1601 War(并查集)
- 枚举数组
- 哈哈哈了啊
- Path类和File类
- ASIHTTPRequest 详解, http 请求终结者
- cocos2d-x 3.4之排行榜的实现
- 曾做过一个IOS仿糗事百科的项目
- 关于onNewIntent的使用&PendingIntent flag赋值
- input框中加入小图标
- 听说苹果手机的一个BUG是无法得到如图的正确结果?