将块强制转换为 void* 以进行动态类方法解析
+(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));
这一行给我一个警告而不是错误 - 语义问题:将“objc_objectptr_t”(又名“const void *”)传递给“void *”类型的参数会丢弃限定符
+(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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不想这么说,但在这种情况下你可能不得不放弃 const 。
IMP myIMP = imp_implementationWithBlock((void*)objc_unretainedPointer(noteFactoryBLOCK));
但这相当难看。
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.