【问题标题】:casting a block to a void* for dynamic class method resolution将块转换为 void* 以进行动态类方法解析
【发布时间】:2011-09-15 12:08:09
【问题描述】:
+(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。

编辑

    IMP myIMP = imp_implementationWithBlock(objc_unretainedPointer(noteFactoryBLOCK));

这一行给我一个警告而不是错误 - Semantic Issue: Passing 'objc_objectptr_t' (aka 'const void *') to parameter of type 'void *' discards qualifiers

【问题讨论】:

标签: objective-c dynamic casting implementation void-pointers


【解决方案1】:

我不想这么说,但在这种情况下你可能只需要抛弃 const。

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

不过这很丑。

【讨论】:

  • 确实很丑。我什至没有真正考虑过这个选项,因为我认为它在某种程度上不是一个选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多