Method resolution priority between categories and class extensions
0 reputation · 12 Aug 2020, 06:25 UTC
Dynamic Dispatch Conflict
Objective-C utilizes a dynamic messaging system where objc_msgSend resolves selectors at runtime. When a class incorporates both category methods and class extensions, the runtime must determine the priority of method resolution, particularly when these definitions overlap within a complex inheritance chain.
The runtime behavior regarding category method collisions is documented: the last category loaded typically overrides previous implementations. However, the interaction between these category-defined methods and methods declared in class extensions or subclasses introduces ambiguity in the dispatch order.
Given a scenario where a method is defined in a category of a superclass and also within a class extension of a subclass, the resolution priority remains a point of design uncertainty.
- Does the class extension take precedence over a category method defined in the same class?
- How does the runtime prioritize a category method in a superclass versus a class extension in a subclass?