NSMutableString *string1 = [NSMutableString initWithFormat:@"Tom"];
NSMutableString *string2 = [NSMutableString initWithFormat:@"Harry"];
NSMutableString *string3 = [NSMutableString initWithFormat:@"Jack"];
NSArray *array = [NSArray arrayWithObjects:string1,string2,string3, nil];
way1- 枚举
// 创建的 s 的类类型必须和需要被枚举的数组中的元素的类型保持一致
for ( NSString *s in array ){
NSLog(@"The string is %@.", s);
}
结果:
The string is Tom.
The string is Harry.
The string is Jack.
way2- 使用 selector (选择器)
[array makeObjectsPerformSelector:@selector(appendingString:) withObject:@"-student"];
way3- 使用块定义
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
NSLog(@"The object(%lu)'s description is %@.", idx, [obj description]);
}];
在方法2 执行后,再执行方法3的结果:
The object(1)'s description is Tom-student.
The object(2)'s description is Harry-student.
The object(3)'s description is Jack-student.
遍历数组—三种内置的方法
最新推荐文章于 2020-11-17 16:10:27 发布