Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 IMP 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

like image 531
griotspeak Avatar asked Jun 15 '11 12:06

griotspeak


1 Answers

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.

like image 195
Joshua Weinberg Avatar answered Sep 19 '22 13:09

Joshua Weinberg