【发布时间】:2011-12-02 18:39:41
【问题描述】:
Mike Ash 创建了一个使用工作表中的blocks to handle callbacks 的示例,看起来非常不错。这又被更新为用户 Enchilada 在beginSheet: block alternative? 的另一个 SO 问题中使用垃圾收集,见下文。
@implementation NSApplication (SheetAdditions)
- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow didEndBlock:(void (^)(NSInteger returnCode))block
{
[self beginSheet:sheet
modalForWindow:docWindow
modalDelegate:self
didEndSelector:@selector(my_blockSheetDidEnd:returnCode:contextInfo:)
contextInfo:Block_copy(block)];
}
- (void)my_blockSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
void (^block)(NSInteger returnCode) = contextInfo;
block(returnCode);
Block_release(block);
}
@end
启用 GC 时,这不适用于自动引用计数 (ARC)。我自己是 ARC 和 blocks 的初学者,无法让它发挥作用。我应该如何修改代码以使其与 ARC 一起使用?
我知道 Block_release() 的东西需要删除,但我无法克服关于将 'void *' 转换为 'void (^)(NSInteger)' 的编译错误,ARC 不允许这样做。
【问题讨论】:
标签: objective-c cocoa automatic-ref-counting objective-c-blocks cocoa-sheet