2

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.

Alex Gosselin
  • 2,942
  • 21
  • 37

2 Answers2

4

You can always look at self->isa.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • Is it still accessible in the new runtime? (checking the headers...) Ah, apparently yes. I didn't know that. +1 – Yuji Jul 16 '11 at 15:15
3

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]!

Yuji
  • 34,103
  • 3
  • 70
  • 88