如果您的块是异步的,则您无法在 return is 之前设置返回值,因为程序可能在方法结束之前还没有完成异步任务。一些更好的解决方案是:
如果您愿意,请找到一个可以完成相同工作的同步方法。
现在这可能不是最佳选择,因为您的 UI 会在块运行时锁定。
找到值时调用选择器,并使方法本身无效。
或许,尝试这样的事情:
-(void) findImageAtIndex:(NSUInteger)index target:(id)target foundSelector:(SEL)foundSEL
{
__block UIImage *image;
[self.album enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] options:0 usingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
//set image in here
if ([target respondsToSelector:foundSEL]) { //If the object we were given can call the given selector, call it here
[target performSelector:foundSEL withObject:image];
return;
}
}];
//Don't return a value
}
然后你可以这样称呼它:
...
//Move all your post-finding code to bar:
[self findImageAtIndex:foo target:self foundSelector:@selector(bar:)];
}
- (void)bar:(UIImage *)foundImage {
//Continue where you left off earlier
...
}