我声明一个变量只是为了限制过滤 NSDictionary 的结果数量。有没有其他方法可以避免额外的变量?
我的代码:
//Pick one part from each item
__block int partsCounter = 0;
NSSet *itemsParts = [self.deckDictionary keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
//filter with itemRef value
CollectiblePart* part = (CollectiblePart*)obj;
if([candidatesSet containsObject:[NSNumber numberWithInt: part.itemRef]]){
if(partsCounter < quantity)
{
partsCounter++;
return YES;
}else{
//stop the filtering
return (*stop = NO);
}
}else{
return NO;
}
}];