Objective-c '-makeObjectsPerformSelector:'

-makeObjectsPerformSelector: 方法

当我们需要对NSArray or NSSet里面的对象,执行同一个操作的时候,我们首先想到的可能是 for 循环。其实对于NSArray 和 NSSet 都有个一简便的方法:

-makeObjectsPerformSelector:,数组里面的元素同时执行某个 selector。-makeObjectsPerformSelector:withObject:可以带一个参数。例如移除某个视图的所有subview,只要一句话:

1
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

既然提到了循环,我们就总结一下,数组中其他几种常用的循环方式:

for in

for (id obj in array){
}

Block Enumeration

[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger     idx, BOOL *stop) {
}];
//Concurrent:
[array enumerateObjectsWithOptions:     NSEnumerationConcurrent usingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { 
}];

GCD

GCD apply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

//sync:
dispatch_apply([array count], queue, ^(size_t idx) {
id obj = [array objectAtIndex: idx];

});

//Async:
dispatch_async(queue, ^(void) {
dispatch_apply([array count], queue, ^(size_t idx) {
id obj = [array objectAtIndex: idx];
});
});