首页 > 其他 > 详细

Objective-C 学习笔记(五) 快速枚举

时间:2018-06-01 12:38:05      阅读:191      评论:0      收藏:0      [点我收藏+]

Objective-C 快速枚举

快速枚举是一个Objective-C的功能,有助于列举一个集合。

快速枚举语法

for (classType variable in collectionObject )
{ 
  statements 
}

实例:

 1 #import <Foundation/Foundation.h>
 2 
 3 int main()
 4 {
 5    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 6    NSArray *array = [[NSArray alloc]
 7    initWithObjects:@"string1", @"string2",@"string3",nil];
 8    for(NSString *aString in array)
 9    {
10       NSLog(@"Value: %@",aString);
11    }
12    [pool drain];
13    return 0;
14 }

得到以下的结果:

Value: string1
Value: string2
Value: string3

 

快速向后枚举语法

for (classType variable in [collectionObject reverseObjectEnumerator])
{
    statements
}

实例:

 1 #import <Foundation/Foundation.h>
 2 
 3 int main()
 4 {
 5    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 6    NSArray *array = [[NSArray alloc] initWithObjects:@"string1","string2",@"string3",nil];
 7    for(NSString *aString in [array reverseObjectEnumerator])
 8    {
 9       NSLog(@"Value: %@",aString);
10    }
11    [pool drain];
12    return 0;
13 }

得到以下的结果:

Value: string3
Value: string2
Value: string1

可以看到在输出中,每个打印的数组中的对象,但以相反的顺序较正常要快的枚举。

 

Objective-C 学习笔记(五) 快速枚举

原文:https://www.cnblogs.com/hyating/p/9120788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!