我正在嵌套块,它看起来 UGGGGLY。有没有办法写这个不那么难看?主要是寻找语法建议,而不是结构性的,但我都会接受。
我的块工厂方法,
-(NSImage *(^)(CGFloat size, BOOL preview))resizeBlock {
return (NSImage *(^)(CGFloat size, BOOL preview))[[^(CGFloat size, BOOL preview){
// image-resizing code
return [[[NSImage alloc] init] autorelease];
} copy] autorelease];
}
从许多与此类似的函数中调用它,
-(void)queueResize:(CGFloat)targetSize toView:(NSImageView *)targetView {
NSImage*(^sizeBlock)(CGFloat,BOOL) = [self resizeBlock];
NSBlockOperation *bo = [NSBlockOperation blockOperationWithBlock:^(void) {
NSImage *previewImage = (NSImage*)sizeBlock(targetSize,YES);
targetView.image = previewImage;
}];
[queue addOperation:bo];
}
queue 是一个 NSOperationQueue 对象。如果没有所有(丑陋的)强制转换,它将无法编译。亚硝酸胺?
编辑:根据 Dave DeLong 的回答和http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/,我改变了行
targetView.image = previewImage;
成为,
[targetView performSelectorOnMainThread:@selector(setImage:) withObject:previewImage waitUntilDone:YES];