Objective-C 运行时:如何从类中删除方法?
在 Objective-C 运行时参考,我看到 class_addMethod
但没有看到 class_removeMethod
。如何动态删除方法?
另外,class_addMethod
是添加实例方法还是类方法?
In the Objective-C Runtime Reference, I see class_addMethod
but not class_removeMethod
. How do I dynamically remove a method?
Also, does class_addMethod
add an instance method or a class method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Inerdial 在他的评论中提到的,主要问题(如何在运行时从类中删除方法?)在某种程度上得到了详尽的回答 此处。
马特迪帕斯夸莱也问:
惯性再次正确:
给定一个类 c,我们可以得到它作为实例的类(称为它的“元类”),就像
然后“向 c 添加一个类方法”一样,我们添加使用 class_addMethod 的 metac 方法。
例如,如果我们已经在其他地方定义了,
那么我们可以向 c 添加一个类方法,如下所示:
或者等效地,
为了简单地将这个相同的方法添加为 c 上的实例方法,我们只需编写
引用:
1. Objective-C 运行时参考 2.Objective-C 运行时编程指南 - 类型编码 3.Cocoa with Love - 什么是 Objective 中的元类-C?
As Inerdial mentioned in his comment, the main question (How can a method be removed from a class at runtime?) is somewhat exhaustively answered here.
MattDiPasquale asks as well:
Inerdial is correct again:
Given a Class c, we can get our hands on the class of which it is an instance (known as its "metaclass") as simply as
To then "add a class method" to c, we add a method to metac using class_addMethod.
If, for example, elsewhere we have already defined
We can then add a class method to c as follows:
or equivalently
To simply add this same method as an instance method on c, we simply write
References:
1. Objective-C Runtime Reference 2. Objective-C Runtime Programming Guide - Type Encodings 3. Cocoa with Love - What is a meta-class in Objective-C?