如何从 Objective-C 调用使用 class_getInstanceMethod 保存的方法?
如何调用我之前使用以下代码保存的方法:
SEL sel = @selector(someMethod:param:);
Method myMethod = class_getInstanceMethod([SomeClass class], sel);
正如您所想,调用 [SomeClass someMethod]
不起作用,因为稍后我会混合原始方法。
How do I call a method that I previously saved using the code below:
SEL sel = @selector(someMethod:param:);
Method myMethod = class_getInstanceMethod([SomeClass class], sel);
As you may imagine, calling [SomeClass someMethod]
is not going to work because, later, I swizzle the original method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将指针类型转换为正确的函数类型,请记住方法有两个隐式参数:self 和 _cmd。来自Apple的 运行时文档:(
编辑)
保留请记住,Method 类型是一个结构体,并且在 ObjC2 运行时中,它是不透明的,因此您无法直接访问其成员 - 您需要使用
method_getImplementation(myMethod)
来获取您可以像上面那样进行类型转换的 IMP。You need to typecast the pointer to the proper function type, keeping in mind that methods have two implicit arguments, self and _cmd. From Apple's runtime docs:
(Edit)
Keep in mind that the Method type is a struct, and in the ObjC2 runtime, it's opaque, so you don't have direct access to its members - you'll need to use
method_getImplementation(myMethod)
to get an IMP that you can typecast like above.