Drop block to void * to allow dynamic class method

+(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); ... 

I get an error on the last line because noteFactoryBLOCK is passed to void *, and ARC forbids this. Is there a way to do what I want? I need an IMP that I can pass class_addMethod at runtime.

EDIT

  IMP myIMP = imp_implementationWithBlock(objc_unretainedPointer(noteFactoryBLOCK)); 

This line gives me a warning instead of an error - Semantic Issue: Passing 'objc_objectptr_t' (aka 'const void *') to parameter of type 'void *' discards qualifiers

+7
source share
1 answer

I'm sorry to say this, but you just need to drop const in this case.

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

It's pretty ugly though.

+2
source

All Articles