2011-01-28 60 views

回答

5

沒關係。我發現我正在尋找在這兩個網站:

http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.htmlhttp://www.cocoabuilder.com/archive/cocoa/281058-sheets-blocks-and-garbage-collector.html

其實,這是代碼,它是既GC和非GC完全兼容:

@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