数组遍历几种方法
来源:互联网 发布:windows ce6.0导航升级 编辑:程序博客网 时间:2024/06/05 15:10
Blog类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import "Blog.h"
@implementation Blog
+(Blog *)blog{
Blog * blog = [[Blog alloc] init];
return blog;
}
-(Blog *)setBlogTitle:(NSString *)title andContent:(NSString *)content{
_title = title;
_content = content;
return self;
}
-(NSString *)description{
return [NSString stringWithFormat:@"blog : title is \"%@\" , and content is \"%@\"", _title,_content ];
}
-(void)dealloc{
NSLog(@"%@被销毁了",self.title);
}
@end
主函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma mark Array数组的四种遍历方法
void testArray(){
Blog *blog1 = [[Blog blog] setBlogTitle:@"Love" andContent:@"I love you"];
Blog *blog2 = [[Blog blog] setBlogTitle:@"Friendship" andContent:@"you are my best friend"];
NSArray *array = [NSArray arrayWithObjects:@"hello",@"world",blog1,blog2, nil];
//第一种遍历:普通for循环
long int count = [array count];
for (int i = 0 ; i < count; i++) {
NSLog(@"1遍历array: %zi-->%@",i,[array objectAtIndex:i]);
}
//第二种遍历:快速for循环,需要有外变量i
int i = 0;
for (id obj in array) {
NSLog(@"2遍历array:%zi-->%@",i,[array objectAtIndex:i]);
i++;
}
//第三种遍历:OC自带方法enumerateObjectsUsingBlock:
//默认为正序遍历
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"3遍历array:%zi-->%@",idx,obj);
}];
//NSEnumerationReverse参数为倒序遍历
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"4倒序遍历array:%zi-->%@",idx,obj);
}];
//第四种遍历:利用枚举
NSEnumerator *en = [array objectEnumerator];
id obj;
int j = 0 ;
while (obj = [en nextObject]) {
NSLog(@"5遍历array:%d-->%@",j,obj);
j++;
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
testArray();
}
return 0;
}
0 0
- 数组遍历几种方法
- PHP数组的几种遍历方法
- php数组的几种遍历方法
- 数组遍历的几种方法
- 数组遍历的几种方法
- JavaScript数组的几种遍历方法
- php遍历数组的几种方法
- PHP中数组遍历常用几种方法
- php中遍历二维数组的几种方法详解
- PHP中遍历数组元素的几种方法
- iOS遍历NSArray数组的几种方法
- 二维数组遍历的几种实现方法
- 关于遍历一个数组的几种方法比较
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- jquery遍历筛选数组的几种方法和遍历解析json对象
- math.random()
- Android性能优化系列——Performance Tips
- 生产消费者模式
- Android程序的执行流程分析
- cocoaPods 的安装和使用
- 数组遍历几种方法
- 重新搭建opencv3.0和vs2010
- poj-2411 Mondriaan's Dream (状态压缩dp)
- linux yum命令详解
- Flash Media Server 优化指南
- CBLAS的安装与使用
- android MulticastLock
- apriori和关联规则算法
- CodeForces 630 I. Parking Lot(组合数学)