Objective-C Fast Enumeration
Fast enumeration is an Objective-C's feature that helps in enumerating through a collection. So in order to know about fast enumeration, we need know about collection first which will be explained in the following section.
Collections in Objective-C
Collections are fundamental constructs. It is used to hold and manage other objects. The whole purpose of a collection is that it provides a common way to store and retrieve objects efficiently.
There are several different types of collections. While they all fulfil the same purpose of being able to hold other objects, they differ mostly in the way objects are retrieved. The most common collections used in Objective-C are:
- NSSet
- NSArray
- NSDictionary
- NSMutableSet
- NSMutableArray
- NSMutableDictionary
If you want to know more about these structures, please refer data storage in Foundation Framework.
Fast enumeration Syntax
for (classType variable in collectionObject ) {
statem ents
}
Here is an example for fast enumeration.
#import <Foundation/Foundation.h>
int main() {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = [[NSArray alloc] initWithObjects:@"string1", @"string2",@"string3",nil];
for(NSString *aString in array)
{
NSLog(@"Value: %@",aString);
}
[pool drain];
return ;
}
Now when we compile and run the program, we will get the following result.
-- ::22.835 demo[] Value: string1
-- ::22.836 demo[] Value: string2
-- ::22.836 demo[] Value: string3
As you can see in the output, each of the objects in the array is printed in an order.
Fast Enumeration Backwards
for (classType variable in [collectionObject reverseObjectEnumerator] )
{
statements
}
Here is an example for reverseObjectEnumerator in fast enumeration.
#import <Foundation/Foundation.h> int main()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = [[NSArray alloc]
initWithObjects:@"string1", @"string2",@"string3",nil];
for(NSString *aString in [array reverseObjectEnumerator])
{
NSLog(@"Value: %@",aString);
}
[pool drain];
return ;
}
Now when we compile and run the program, we will get the following result.
-- ::51.025 demo[] Value: string3
-- ::51.025 demo[] Value: string2
-- ::51.025 demo[] Value: string1
As you can see in the output, each of the objects in the array is printed but in the reverse order as compared to normal fast enumeration.
Objective-C Fast Enumeration的更多相关文章
- [报错]Fast enumeration variables cannot be modified in ARC by default; declare the variable __strong to allow this
今天写了下面的快速枚举for循环代码,从按钮数组subButtons中取出button,然后修改button的样式,在添加到view中 for (UIButton *button in subButt ...
- Fast Enumeration
在 Objective-C 2.0 中提供了快速枚举的语法,它是我们遍历集合元素的首选方法.它具有以下优点: 比直接使用 NSEnumerator 更高效: 语法非常简洁: 如果集合在遍历的过程中被修 ...
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- Objective-C官方文档 协议
版权声明:原创作品,谢绝转载!否则将追究法律责任. 在现实生活中,当处理某一情况的时候人们往往遵循严格的程序.执法人员他们在打官司的收集证据和询问的时候一定要遵守协议. 在面向对象的语言中,最重要的是 ...
- Objective-C 高性能的循环
Cocoa编程的一个通常的任务是要去循环遍历一个对象的集合 (例如,一个 NSArray, NSSet 或者是 NSDictionary). 这个看似简单的问题有广泛数量的解决方案,它们中的许多不乏 ...
- Objective-C 高性能的循环遍历 forin - NSEnumerator - 枚举 优化
Cocoa编程的一个通常的任务是要去循环遍历一个对象的集合 (例如,一个 NSArray, NSSet 或者是 NSDictionary). 这个看似简单的问题有广泛数量的解决方案,它们中的许多不乏 ...
- 集合类(Objective-C & Swift)
内容提要: 本文前两部分讲了Cocoa的集合类和Swift的集合类,其中Cocoa提供的集合类包括NSArray.NSMutableArray.NSDictionary.NSMutableDictio ...
- 词典对象 NSDictionary与NSMutableDictionary
做过Java语言或者 C语言开发的朋友应该很清楚关键字map 吧,它可以将数据以键值对儿的形式储存起来,取值的时候通过KEY就可以直接拿到对应的值,非常方便,是一种非常常用的数据结构.在Objecti ...
- IOS常用加密GTMBase64
GTMDefines.h // // GTMDefines.h // // Copyright 2008 Google Inc. // // Licensed under the Apache Lic ...
随机推荐
- C++之引用&的详解
C++中的引用: 引用引入了对象的一个同义词.定义引用的表示方法与定义指针相似,只是用&代替了*.引用(reference)是c++对c语言的重要扩充.引用就是某一变量(目标)的一个别名,对引 ...
- poj2828 Buy Tickets——倒序处理
题目:http://poj.org/problem?id=2828 这题可以倒序来做,因为越靠后的人实际上优先级越高: 用0和1表示这个位置上是否已经有人,0表示有,1表示没有,这样树状数组维护前缀和 ...
- Gulp简单应用
1.创建一个工程,在webstorm控制台 cnpm install --save-dev gulp cnpm install --save-dev gulp-concat ...
- $.ajax数据传输成功却执行失败的回调函数
这个问题迷惑了我好几天,都快要放弃了,功夫不负有心人,最终成功解决,下面写一下我的解决方法. 我传的数据是json类型的,执行失败的回调函数是因为从后台传过来的数据不是严格的json类型,所以才会不执 ...
- junit4 Assert静态方法及API
junit中的assert方法全部放在Assert类中. 1.assertTrue/False([String message,]boolean condition); 用来查看变量是是否为fa ...
- Tomcat自定义classLoader加密解密
class很好反编译,所以需要对class文件先进行加密,然后使用自己的classloader进行解密并加载. [步骤] 大概分两步: 1.对class文件进行加密 2.写解密class文件并加载的c ...
- JSON 下 -- jansson 示例
JSON 下 —— jansson 示例 参考网址: jansson 库的下载: http://www.digip.org/jansson/ 安装jansson 步骤: http://blog.csd ...
- ZOJ3228【AC自动机】
先贡献几个数据(没用别怪我): /* ab 4 0 ab 1 ab 0 ab 1 ab abababac 4 0 aba 1 aba 0 abab 1 abab abcdefghijklmnopqrs ...
- hrbust1444 逃脱 【BFS】
Description 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园某处的 ...
- Unity3D asset bundle 格式简析
http://blog.codingnow.com/2014/08/unity3d_asset_bundle.html Unity3D 的 asset bundle 的格式并没有公开.但为了做更好的差 ...