Dynamic typing, Objective-C, how does it work?

ε祈祈猫儿з 提交于 2019-11-30 02:13:04

"Under the hood", so to speak, all Objective-C objects are C structs with a pointer to the Class object that represents their type. An id is a pointer to the most basic such struct, which looks something like this:

struct objc_object {
    Class isa;
}

id is treated specially by the compiler, in that the compiler doesn't give you any warning that the object may not respond to any selector as it does when you use a more strongly typed variable.

When you call a method on any object, it follows that isa pointer to the Class object and looks in that Class object to find the implementation function for the selector of the method you tried to call.

To add to Anomie's answer, how a class stores the table of which messages it answers to and which bits of code those messages invoke is entirely opaque. So there's an extent to which nobody can answer exactly how Apple's implementation works, and it's almost certain to differ from GNU's implementation.

However, Apple's Objective-C Runtime Reference explains everything that the runtime can do, at the C level. So you can see there what operations are possible for setting and looking things up. It's a relatively simple system under the hood, primarily just a bunch of dictionaries (in the uninflected sense) that map one thing to another, such as from a selector to an IMP function pointer. If there's an entry in the table for a particular class then the relevant thing is called. If not then superclasses are checked, the standard forwardingTargetForSelector: fallback mechanism is considered, and so on.

Beyond that, more specific comments require more specific questions. There are lots of details like that key-value observing is achieved by a method swizzle (so, the runtime adjusts the C pointer that the class will call for the setter to be one that calls the real setter and informs whoever is observing), but they're all just particular uses of the runtime as documented.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!