Objective-C使用动态绑定,但是如何? [英] Objective-C uses dynamic binding, but how?
问题描述
我知道Objective-C对所有方法调用都使用动态绑定.如何实施?在编译之前,objective-c会变成C代码"并且仅对所有内容使用(void *)指针吗?
I know that Objective-C uses dynamic binding for all method calls. How is this implemented? Does objective-c "turn into C code" before compilation and just use (void*) pointers for everything?
推荐答案
从概念上讲,正在发生的事情是存在一个调度程序库(通常称为Objective C运行时),并且编译器将进行如下转换: /p>
Conceptually, what is going on is that there is a dispatcher library (commonly referred to as the Objective C runtime), and the compiler converts something like this:
[myObject myMethodWithArg:a andArg:b ];
进入
//Not exactly correct, but close enough for this
objc_msgSend(myObject, "myMethodWithArg:andArg:", a, b);
然后,运行时处理所有绑定和分派,找到合适的函数,并使用这些args对其进行调用.简而言之,您可以认为它类似于哈希查找.当然,要比实际要复杂得多.
And then the runtime deals with all the binding and dispatch, finds an appropriate function, and calls it with those args. Simplistically you can think of it sort of like a hash lookup; of course it is much more complicated that then in reality.
还有更多与方法签名等问题相关的问题(C不对类型进行编码,因此运行时需要对其进行处理).
There are a lot more issues related to things like method signatures (C does not encode types so the runtime needs to deal with it).
这篇关于Objective-C使用动态绑定,但是如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!