Accessing a method in a super class when it's not exposed

前端 未结 3 1379
孤城傲影
孤城傲影 2020-12-16 12:59

In a subclass, I\'m overriding a method that is not exposed in the super class. I know that I have the correct signature as it is successfully overriding the superclass imp

3条回答
  •  隐瞒了意图╮
    2020-12-16 13:25

    This doesn't work because you're only sending performSelector:, not the selector you pass to that, to the superclass. performSelector: still looks up the method in the current class's method list. Thus, you end up with the same subclass implementation.

    The simplest way to do this may be to just write in your own call to objc_msgSendSuper():

    // Top level (this struct isn't exposed in the runtime header for some reason)
    struct objc_super
    {
        id __unsafe_unretained reciever;
        Class __unsafe_unretained superklass;
    };
    
    // In the subclass's method
    struct objc_super sup = {self, [self superclass]};
    objc_msgSendSuper(&sup, _cmd, other, args, go, here);
    

    This can cause problems in the general case, as Rob Napier has pointed out below. I suggested this based on the assumption that the method has no return value.

提交回复
热议问题