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 ...
随机推荐
- telnet命令发送邮件
下面的例子是用qq的smtp服务器. set localecho 本地回显启用 smtp.qq.com Esmtp QQ Mail Server helo sis smtp.qq.com//服务器返回 ...
- Winform中ComBox大小设置
combox,listview等一些控件的外观会由于字体大小的改变而改变.修改字体的大小宽度自动变化
- springboot中使用@Value读取配置文件
一.配置文件配置 直接配置 在src/main/resources下添加配置文件application.properties 例如修改端口号 #端口号 server.port=8089 分环境配置 在 ...
- docker 学习(四) springboot + docker
下面演示: 在Windows上新建一个简单的Springboot工程,生成docker iamge,然后在本地的docker上运行: (1):登录到 https://start.spring.io/, ...
- Bean的不同配置方式比较与应用场景
基于XML配置 Bean的定义: 在XML文件中通过<bean>元素定义. Bean的名称: 通过<bean>的id或name属性定义. ...
- Android开发相关工具(eclipse篇)
ADT 安装该工具后才能配置Android SDK包,使可以在eclipse里开发Android程序 AVD Android模拟器管理工具,创建删除Android模拟器 SDK Manager And ...
- 2.SJ-SLAM-14
第三讲 三维空间刚体运动 本讲目标 理解三维空间的刚体运动描述方式:旋转矩阵.变换矩阵.四元数和欧拉角. 掌握Eigen库的矩阵.几何模块使用方法. 3.1 点.向量和坐标系,旋转矩阵 二维空间与三维 ...
- golang http/transport 代码分析
请结合源码阅读,本文只是总结一下,源码里有详细的注释.基于:go1.12.4 http.Client 表示一个http client端,用来处理HTTP相关的工作,例如cookies, redirec ...
- HDU1501【简单DP】
题意: 从a串和b串中顺序地取出是否能组成c串: 思路: dp[i][j] 代表从a串中取了i个,b串中取了j个的情况: #include <bits/stdc++.h> using na ...
- Mecanim Control
http://www.ufe3d.com/doku.php/mecanimcontrol Mecanim Control Your ultimate solution for Mecanim base ...