开发者

casting a block to a void* for dynamic class method resolution

开发者 https://www.devze.com 2023-03-12 23:26 出处:网络
+(BOOL)resolveClassMethod:(SEL)aSel { NSString *lString = NSStringFromSelector(aSel); if ([self validateLetterAndAccidental:lString]) {
+(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消