如何检测我拥有的对象是否真的是 NSProxy?

发布于 2024-11-24 22:47:06 字数 187 浏览 1 评论 0 原文

我正在研究苹果 API 向我提供的一些更可疑的对象(例如 mutableArrayValueForKeyPath),这让我想知道苹果多久给我一次我认为是某个对象的东西,但实际上只是一个 NSProxy ,它也秘密地通知其他对象,或过滤我的消息。

有没有办法判断一个对象是否是 NSProxy 的子类?看起来他们非常擅长精确模仿他们所代表的一切。

I'm poking around a bit in some of the more suspicious objects apple APIs hand out to me, (like mutableArrayValueForKeyPath) and it got me wondering how often apple gives me what I believe to be a certain object, but is really just an NSProxy, which is secretly also notifying other objects, or filtering my messages.

Is there a way to tell if an object is a subclass of NSProxy? It seems like they are pretty good at exactly imitating whatever they represent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-12-01 22:47:06

您可以随时查看self->isa

You can always look at self->isa.

感受沵的脚步 2024-12-01 22:47:06

要判断它是否是 NSProxy,请使用 isProxy,请参阅 参考

但还要注意,Apple 使用了许多其他内部魔法,而不仅仅是 NSProxy。例如,为了实现 KVC/KVO,他们经常创建一个关于其自己的类的匿名类,将其插入到类层次结构中,并更改对象的标识。例如,请参阅 Mike Ash 在这篇 周五问答文章中的精彩讨论。 (顺便说一句,如果您对 Objective-C 运行时感到好奇,您绝对应该阅读此博客。)

正如其中所写,揭示对象真实身份的最可靠方法是使用运行时,请参阅 此处

    id obj;
    Class c=object_getClass(obj);

为您提供真正的课程。请注意,对象可以通过重新实现[obj class]来掩盖其本质!

To tell if it's an NSProxy or not, use isProxy, see the reference.

But note also that Apple uses many other internal magics, not just NSProxy. For example, to implement KVC/KVO, they often create an anonymous class which lies about its own class, insert it into the class hierarchy, and change an object's identity. See e.g. a nice discussion by Mike Ash at this Friday Q&A article. (You should definitely read this blog if you're curious about Objective-C runtime, by the way.)

As written there, the most robust way to reveal the true identity of an object is to use the runtime, see here.

    id obj;
    Class c=object_getClass(obj);

gives you the true class. Note that an object can lie what it is by re-implementing [obj class]!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文