casting a block to a void* for dynamic class method resolution
+(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 at the last line because noteFactoryBLOCK is cast to a void* and ARC disallows that. Is there currently a way to accomplish what I want? I would like an IM开发者_Python百科P that I can pass to class_addMethod at runtime.
EDIT
IMP myIMP = imp_implementationWithBlock(objc_unretainedPointer(noteFactoryBLOCK));
This line give me a warning instead of an error - Semantic Issue: Passing 'objc_objectptr_t' (aka 'const void *') to parameter of type 'void *' discards qualifiers
I hate to say it but you might just have to cast away the const in this case.
IMP myIMP = imp_implementationWithBlock((void*)objc_unretainedPointer(noteFactoryBLOCK));
That is pretty ugly though.
精彩评论