转自:http://blog.youkuaiyun.com/liulushi_1988/article/details/14105137
objective-c
语言 数组遍历的4种方式:1、普通for循环;2、快速for循环;3、特性block方法;4、枚举方法。
Blog类:
05 |
Blog
* blog = [[Blog alloc] init]; |
09 |
-(Blog
*)setBlogTitle:(NSString *)title andContent:(NSString *)content{ |
15 |
-(NSString
*)description{ |
16 |
return [NSString
stringWithFormat:@"blog
: title is \"%@\" , and content is \"%@\"",
_title,_content ]; |
20 |
NSLog(@"%@被销毁了",self.title); |
主函数:
01 |
#pragma
mark Array数组的四种遍历方法 |
03 |
Blog
*blog1 = [[Blog blog] setBlogTitle:@"Love" andContent:@"I
love you"]; |
04 |
Blog
*blog2 = [[Blog blog] setBlogTitle:@"Friendship" andContent:@"you
are my best friend"]; |
05 |
NSArray
*array = [NSArray arrayWithObjects:@"hello",@"world",blog1,blog2,
nil]; |
08 |
long int count
= [array count]; |
09 |
for (int i
= 0 ; i < count; i++) { |
10 |
NSLog(@"1遍历array:
%zi-->%@",i,[array
objectAtIndex:i]); |
15 |
for (id
obj in array) { |
16 |
NSLog(@"2遍历array:%zi-->%@",i,[array
objectAtIndex:i]); |
23 |
[array
enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{ |
24 |
NSLog(@"3遍历array:%zi-->%@",idx,obj); |
27 |
[array
enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{ |
28 |
NSLog(@"4倒序遍历array:%zi-->%@",idx,obj); |
32 |
NSEnumerator
*en = [array objectEnumerator]; |
35 |
while (obj
= [en nextObject]) { |
36 |
NSLog(@"5遍历array:%d-->%@",j,obj); |
40 |
int main(int argc, const char *
argv[]) |