Mike Ash创建了一个示例,使用块处理来自sheets的回调,这似乎非常好。然后,用户Enchilada在另一个SO问题中更新了它以与垃圾收集一起使用,该问题位于beginSheet:block alternative?,请参见下文。
@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
启用垃圾回收时,这与自动引用计数(ARC)不兼容。作为一个同时对ARC和blocks都是初学者的人,我无法让它正常工作。我应该如何修改代码才能使其与ARC兼容?
我知道Block_release()需要去掉,但我无法解决编译错误,因为在ARC中将'void *'转换为'void (^) (NSInteger)'被禁止。