I know how to use categories as a programmer, but I'm curious how they are implemented. Does the compiler compile them into calls to class_replaceMethod from a static initializer? Thanks.
New answer on topic.
Each class has a list of methods, when doing a method lookup the method list is scanned from beginning to end. If no method is found the superclass' list is scanned, etc. until reaching the root class. Found methods are cached for faster lookup next time.
When loading a category onto a class the categories method list is prepended to the existing list, and caches are flushed. Since the list is searched sequentially this means that the categories method will be found before the original method on next search.
This setup of categories is done lazily, from static data, when the class is first accessed. And can be re-done if loading a bundle with executable code.
In short it is a bit more low level than class_replaceMethod()
.
You can find everything you ever wanted to know about how they work from here.
http://opensource.apple.com/source/objc4/objc4-493.9/runtime/objc-runtime-new.mm
The runtime is fully open source.
Categories do not have any special implementation, they are in fact by nature void of implementation.
The protocols acts as markers on classes at run-time. You can use class_copyProtocolList()
to get the list of protocols that a class conforms to from the runtime. There is a sibling protocol_copyProtocolList()
function to get the protocol that a protocol conforms to.
Do notice that these methods only returns a list of protocols for this particular class or protocol. Not from superclasses, or other protocols by reference. This means that the actual lookup at run-time will be expensive. Instead use class_conformsToProtocol()
(or protocol_conformsToProtocol()
) to query for conformance, these methods can cache the result.
In practice querying for conformance at run-time is seldom a good idea. The protocol conformance is validated with warnings by the compiler, if the developer chooses to ignore these warning, well... it's their choice let them.
来源:https://stackoverflow.com/questions/7026259/how-are-categories-implemented-in-objective-c