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 ...
随机推荐
- PHP 单引号与双引号的区别 SQL中的使用
php单引号与双引号用法:引号嵌套方法 1.双引号内不能直接就再嵌套双引号 2.双引号与单引号互相嵌套使用 如: 双引号内直接嵌套单引号 echo "<script language= ...
- vue watch 深度监听以及立即监听
vue watch对象可以监听数据,数据发生变化,处理函数 watch虽可以监听,但只是浅监听,只监听数据第一层或者第二层.比如对于整个对象的监听,需要用到深度监听 vm.$watch('obj',f ...
- openstack封装镜像
1.准备工作:准备你想要封装的各种镜像的iso,完整版本最简单版本都ok,只要能出虚拟机就行,这个大家去官网下载自己要的iso就可以,我这里用centos6.4最简版本,因为分给我的活让做这个的... ...
- project工期出现小数问题
进入“选项”,点击“日程”,将默认“开始”.“结束”时间调整为“9点~18点.原为PREJECT 日历 (注意是日历~!!)默认的“8点~17点”,与preject系统默认时间“9点~18点”有差别~ ...
- Bishops
题意: 给定一个 $n*n$ 的国际棋盘,求问在上面放 $K$ 个象的方案数. 解法: 首先可以发现黑格和白格互不干扰,这样我们可以将黑格,白格分别求出. 考虑 $f(i,j)$ 表示坐标化后考虑长度 ...
- 1.1-1.4 hadoop调度框架和oozie概述
一.hadoop调度框架 Linux Crontab Azkaban https://azkaban.github.io/ Oozie http://oozie.apache.org/ Zeus(阿里 ...
- GIL 已经被杀死了么?
GIL 已经被杀死了么? 本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/8KvQemz0SWq2hw-2aBPv2Q ...
- HTML5 中的meter 标签的样式设置
meter { -webkit-appearance: none; position: relative; display: block; margin: 8px auto; width: 100px ...
- HDU5894【组合数学】
题意: 现在 m个考生人需要坐在有n个座位的圆桌上. 你需要安排位置,使得任意两个考生之间相距至少k个位置. 桌子有编号,考生a和b交换位置视作一种方案,问有多少方案,mod 1e9+7. (0 &l ...
- P4091 [HEOI2016/TJOI2016]求和(第二类斯特林数+NTT)
传送门 首先,因为在\(j>i\)的时候有\(S(i,j)=0\),所以原式可以写成\[Ans=\sum_{i=0}^n\sum_{j=0}^nS(i,j)\times 2^j\times j! ...