//第一种遍历:普通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++;
}
objective-c遍历
最新推荐文章于 2024-08-23 21:57:41 发布