2011-06-15 63 views
7
+(BOOL)resolveClassMethod:(SEL)aSel { 
    NSString *lString = NSStringFromSelector(aSel); 

    if ([self validateLetterAndAccidental:lString]) { 

     id (^noteFactoryBLOCK)(id) = ^(id aSelf) { 
      return [self noteWithString:lString]; 
     }; 

     IMP lIMP = imp_implementationWithBlock(noteFactoryBLOCK); 
     ... 

我在最後一行得到一個錯誤,因爲noteFactoryBLOCK被轉換爲void *而ARC不允許這樣做。目前有什麼方法可以完成我想要的?我想要一個可以在運行時傳遞給class_addMethod的IMP。將一個塊投射到一個void *用於動態類方法分辨率

編輯

IMP myIMP = imp_implementationWithBlock(objc_unretainedPointer(noteFactoryBLOCK)); 

此行給我一個警告而不是錯誤 - Semantic Issue: Passing 'objc_objectptr_t' (aka 'const void *') to parameter of type 'void *' discards qualifiers

+0

ARC現已公開!請參閱http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-June/015588.html – 2011-06-15 23:01:52

回答

2

我討厭這樣說,但你可能只需要拋棄的常量在這種情況下。

IMP myIMP = imp_implementationWithBlock((void*)objc_unretainedPointer(noteFactoryBLOCK));

這是相當難看,但。

+0

確實很醜。我甚至沒有真正考慮過這個選擇,因爲我認爲這不是一種選擇。 – griotspeak 2011-08-03 17:34:02

相關問題