objective-cnsproxy

How can I detect if an object I have is really an 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.


Solution

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