NSArray提供三种解决方案遍历对象列表:
1.内置循环for-each:
<span style="white-space:pre"> </span>for(NSMutableString *s in listOfObjects){
<span style="white-space:pre"> </span>NSLog(@"%@",[s lowercaseString]);
<span style="white-space:pre"> </span>}
2.使用makeObjectsPerformSelector:或makeObjectsPerformSelector:withObject: 方法:
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
这是 NSArray和NSSet的两个方法,相信大家很少用,它类似于 for循环,但有效率高于for循环
makeObjectsPerformSelector:类似于NSNotifation机制,并发的执行同一件事,不能像for循环那样区别对待
所以参数 argument 必须是非基本类型 ,如果要是用基本类型 请转换程 NSNumber 或者NSValue
3.通过Blocks的enumerateObjectsUsingBlock方法:
<span style="white-space:pre"> </span>[listOfObjects enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
<span style="white-space:pre"> </span> NSLog(@"object(%lu)'s description is %@",idx,[obj description]);
<span style="white-space:pre"> </span>];