2
我正在使用Objective-C块,但是我在理解下面的代码执行过程中遇到了麻烦。块的执行流程是什么?
下面是代码:
NSArray *array = @[@"A", @"B", @"C", @"A", @"B", @"Z", @"G", @"are", @"Q"];
NSSet *filterSet = [NSSet setWithObjects: @"A", @"Z", @"Q", nil];
BOOL (^test)(id obj, NSUInteger idx, BOOL *stop);
test = ^(id obj, NSUInteger idx, BOOL *stop) {
if (idx < 5) {
if ([filterSet containsObject: obj]) {
return YES;
}
}
return NO;
};
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:test];
NSLog(@"indexes: %@", indexes);
输出:
indexes: <NSIndexSet: 0x10236f0>[number of indexes: 2 (in 2 ranges), indexes: (0 3)]
在该方法中,[array indexesOfObjectsPassingTest:test];
,所述test
块是我传递的参数。
但是在上面的块中,test = ^(id obj, NSUInteger idx, BOOL *stop)
参数obj
,idx
和stop
的值是多少?他们从哪里来?
将idx索引值自动调整为像idx = 0,idx = 1,并很快......。 – Prince 2013-05-03 12:32:00
这是正确的。 – 2013-05-03 12:33:46
谢谢,我刚刚执行,它自动采取上述格式..非常感谢你.... – Prince 2013-05-03 12:42:44