#pragma mark 遍历数组1
void arrayFor1() {
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil];
int count = array.count;
for (int i = 0; i<count;i++){
id obj = [array objectAtIndex:i]; }
}
#pragma mark 遍历数组2 快速遍历
void arrayFor2() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil];
int i =0;
for (id obj in array) {
NSLog(@"%i-%@", i, obj);
i++;
}
}
#pragma mark 遍历数组3
void arrayFor3() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil];
[array enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOLBOOL *stop) {
NSLog(@"%i-%@", idx, obj);
// 如果索引为1,就停止遍历
if (idx == 1) {
// 利用指针修改外面BOOL变量的值
*stop = YES;
}
}];
}
#pragma mark 遍历数组4
void arrayFor4() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil];
// 获取数组的迭代器
// NSEnumerator *enumerator = [array objectEnumerator];
// 反序迭代器(从尾部开始遍历元素)
NSEnumerator *enumerator = [array reverseObjectEnumerator];
// allObjects是取出没有被遍历过的对象
NSArray *array2 = [enumerator allObjects];
NSLog(@"array2:%@", array2);
// 获取下一个需要遍历的元素
id obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj=%@", obj);
}
}