Mike Ash创建了一个使用块来处理来自工作表的回调的示例,这看起来非常好.这反过来更新为用户Enchilada在beginSheet的另一个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
Run Code Online (Sandbox Code Playgroud)
启用GC时,这不适用于自动引用计数(ARC).我自己,作为ARC和街区的初学者,都无法让它发挥作用.我应该如何修改代码以使其与ARC一起使用?
我知道Block_release()的东西需要去,但我无法通过关于使用ARC禁止将'void*'转换为'void(^)(NSInteger)'的编译错误.
cocoa objective-c objective-c-blocks automatic-ref-counting cocoa-sheet