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的更多相关文章

  1. [报错]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 ...

  2. Fast Enumeration

    在 Objective-C 2.0 中提供了快速枚举的语法,它是我们遍历集合元素的首选方法.它具有以下优点: 比直接使用 NSEnumerator 更高效: 语法非常简洁: 如果集合在遍历的过程中被修 ...

  3. Objective - c Foundation 框架详解2

    Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...

  4. Objective-C官方文档 协议

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 在现实生活中,当处理某一情况的时候人们往往遵循严格的程序.执法人员他们在打官司的收集证据和询问的时候一定要遵守协议. 在面向对象的语言中,最重要的是 ...

  5. Objective-C 高性能的循环

    Cocoa编程的一个通常的任务是要去循环遍历一个对象的集合  (例如,一个 NSArray, NSSet 或者是 NSDictionary). 这个看似简单的问题有广泛数量的解决方案,它们中的许多不乏 ...

  6. Objective-C 高性能的循环遍历 forin - NSEnumerator - 枚举 优化

    Cocoa编程的一个通常的任务是要去循环遍历一个对象的集合  (例如,一个 NSArray, NSSet 或者是 NSDictionary). 这个看似简单的问题有广泛数量的解决方案,它们中的许多不乏 ...

  7. 集合类(Objective-C & Swift)

    内容提要: 本文前两部分讲了Cocoa的集合类和Swift的集合类,其中Cocoa提供的集合类包括NSArray.NSMutableArray.NSDictionary.NSMutableDictio ...

  8. 词典对象 NSDictionary与NSMutableDictionary

    做过Java语言或者 C语言开发的朋友应该很清楚关键字map 吧,它可以将数据以键值对儿的形式储存起来,取值的时候通过KEY就可以直接拿到对应的值,非常方便,是一种非常常用的数据结构.在Objecti ...

  9. IOS常用加密GTMBase64

    GTMDefines.h // // GTMDefines.h // // Copyright 2008 Google Inc. // // Licensed under the Apache Lic ...

随机推荐

  1. moco实例

    一.moco模拟接口响应json moco的下载地址见虫师博客园:https://www.cnblogs.com/fnng/p/7511539.html foo.json文件内容如下 [ { &quo ...

  2. hibernate 学习 一 基本概念

    1: Hibernate对JDBC进行封装,以面向对象的方式对关系型数据库进行操作. 2:  Hibernate的配置文件: hibernate.properties  或者  hibernate.c ...

  3. Linux安装ntp同步时间

    1.安装 yum install  ntp 安装下就可以了. 2.寻找一个网络时间服务器,比如一些国家授时中心 微软公司授时主机(美国) time.windows.com 台警大授时中心(台湾) as ...

  4. World CodeSprint 10

    C: 题意: 给定一个长度为 $n$ 的序列 $a_i$,从 $a$ 序列中选出一个大小为 $k$ 的子序列使得子序列数字的  bitwise AND 值最大. 求问最大值是多少,并求出有多少个最大值 ...

  5. Yet Another Number Sequence

    题意: $F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}$ 求解$\sum_{i=1}^n{ F_i i^K } \  mod \  10^9+7$. 解法: 记$ ...

  6. Flutter从入门到进阶实战携程网App_汇总贴

    视频地址:https://coding.imooc.com/class/321.html?mc_marking=60e5294c605a87b2af7257d06f70505e&mc_chan ...

  7. FLINK源代码调试方式

    此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 第一种,直接通过IDEA WINDOWS调试 前提是Flink所有依赖已经导入,直接在Test中打断点,然后直 ...

  8. npm和package.json那些不为常人所知的小秘密

    此文已由作者黄锴授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 强大的命令功能 如果你没使用过script,那你可算是从来没手动编辑过package.json.script作 ...

  9. 【阿里云IoT+YF3300】1.时代大背景下的阿里云IoT物联网的现状和未来

    “未来十到二十年,大家基本已经形成了一个共识,那便是新格局的奠定将由 AI 和物联网技术来支撑.放眼国内,在这些互联网巨头之中,未来真正成为竞争对手厮杀的,阿里和华为是首当其冲,在这两个领域双方分别暗 ...

  10. vijosP1286座位安排(状压DP)

    传送门 题意 计算\(C_{n*m}^k/可行方案数\) 分析 定义dp[i][j][k]为第i行用过人数为j个且第i行状态为k的方案数 转移方程:dp[i][j][k]=Σdp[i-1][j-num ...