OC数组遍历

/***************************数组遍历******************************/

NSArray *array = @[@"111", @"222", @"333", @"444"];

// 1.for 循环

for (int i=0; i

// 2.for in 循环

for (NSString *str in array) {

NSLog(@"%@", str);

}

// 3.迭代遍历

[array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

if (idx == 1) {

*stop = YES;

}

NSLog(@"%@", obj);

}];


/***************************数组中方法******************************/

Person *p1 = [[Person alloc] init];

Person *p2 = [[Person alloc] init];

Person *p3 = [[Person alloc] init];

Person *p4 = [[Person alloc] init];

NSArray *array = @[p1, p2, p3, p4];

// 方法一

[array enumerateObjectsUsingBlock:^(Person* obj, NSUInteger idx, BOOL * _Nonnull stop) {

[obj say];

}];

// 方法二

// [array makeObjectsPerformSelector:@selector(say)];

你可能感兴趣的:(OC数组遍历)