我对 Objective-C 中的块有疑问。
例如我有这个代码:
__block int count = 0;
void (^someFunction)(void) = ^(void){
count = 4;
};
count +=2;
编写同一段代码的正确方法是什么,所以计数将变为 6,而不是 2?!
谢谢!
我可能应该显示实际代码,因为我之前的问题很模糊。编辑:
__block CMTime lastTime = CMTimeMake(-1, 1);
__block int count = 0;
[_imageGenerator generateCGImagesAsynchronouslyForTimes:stops
completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime,
AVAssetImageGeneratorResult result, NSError *error)
{
if (result == AVAssetImageGeneratorSucceeded)
{
NSImage *myImage = [[NSImage alloc] initWithCGImage:image size:(NSSize){50.0,50.0}];
[arrOfImages addObject:myImage];
}
if (result == AVAssetImageGeneratorFailed)
{
NSLog(@"Failed with error: %@", [error localizedDescription]);
}
if (result == AVAssetImageGeneratorCancelled)
{
NSLog(@"Canceled");
}
if (arrOfImages.count > 5)
{
NSLog(@"here");
}
count++;
}];
int f = count+1;
10 次迭代后计数为 0...为什么?!?!