This is fairly straightforward to test:
if (method_getImplementation(class_getInstanceMethod(A, @selector(methodRed))) ==
method_getImplementation(class_getInstanceMethod(B, @selector(methodRed))))
{
// B does not override
}
else
{
// B overrides
}
I do have to wonder how knowing if B overrides a method on A is helpful, but if you want to know, this is how you find out.
It also may be worth noting: In the strictest terms the above code determines whether the implementation for the selector on B is different from the implementation of the selector on A. If you had a hierarchy like A > X > B and X overrode the selector, this would still report different implementations between A and B, even though B wasn't the overriding class. If you want to know specifically "does B override this selector (regardless of anything else)" you would want to do:
if (method_getImplementation(class_getInstanceMethod(B, @selector(methodRed))) ==
method_getImplementation(class_getInstanceMethod(class_getSuperclass(B), @selector(methodRed))))
{
// B does not override
}
else
{
// B overrides
}
This, perhaps obviously, asks the question "does B have a different implementation for the selector than its superclass" which is (perhaps more specifically) what you asked for.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…