如何获取 Objective-C 中给定类的当前实例化对象的列表?
由于 class_poseAs(..) 在 Objective-C 2.0 中已被弃用,因此我需要找到另一种方法来在运行时更改对象的类。我发现我可以使用 object_setClass(..) 更改对象的类。我现在的问题是查找给定类的所有当前实例以便更新它们。
一个解决方案是维护一个我想要更新的实例的全局字典,但我想知道是否已经有一种方法可以从反射 api 获取这样的集合。
我检查了 Apple 的运行时参考 并我找不到任何有用的东西。仅使用 method_exchangeImplementations(..) 更新类的方法对我来说不是一个解决方案,因为我希望能够通过使用 super 重用旧实现。
Since class_poseAs(..) is deprecated in Objective-C 2.0, I need to find another way to change the class of an object at runtime. I've found I can change an object's class using object_setClass(..). My problem now is finding all the current instances of a given class in order to update them.
A solution would be to maintain a global dictionary of the instances I want to potentially update, but I'd like to know whether there's already a way to obtain such a collection from the reflective api.
I checked Apple's Runtime reference and I cannot find anything useful. Updating just the method of a class with method_exchangeImplementations(..) is not a solution for me since I want to be able to reuse the old implementation by using super.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,如果您想将特定类中各种方法的实现交换为您自己的,您可以使用 method_exchangeImplementations() 函数。 (您需要
#import objc/runtime.h
)。这比尝试换出实际的类要简单得多,因为poseAsClass:
已被弃用并且在 64 位运行时中不可用。Generally if you want to exchange the implementation of various methods in a particular class for your own, you'd use the method_exchangeImplementations() function in the Objective-C 2.0 runtime. (You'll need to
#import objc/runtime.h
). This is far simpler than trying to swap out the actual class, asposeAsClass:
is deprecated and unavailable in the 64-bit runtime.