Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to call a method of super class of a super class, without breaking the inheritance chain. Something like this:

+(id) alloc 
{
    return [super.super alloc];
}

Is there a way to achieve this ?

Do not confuse with behavior offering by superclass method, discussed here.


UPD:

A few words about super and superclass differences.

Lets say, we have AClass and SuperAClass. As follows from their names AClass inherits SuperAClass. Each of them has an implementation of a method -(void) foo;

AClass implements one of the following class methods:

1. superclass:

+(id) alloc {
    return [[self superclass] alloc];
}

2. super:

+(id) alloc {
    return [super alloc];
}

Now, suppose these 2 lines of code:

AClass *AClassInstance = [AClass alloc];
[AClassInstance foo];

In first case (using superclass), SuperAClass's foo method will be called. For the second case (using super), AClass's foo method will be called.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
559 views
Welcome To Ask or Share your Answers For Others

1 Answer

In your particular example, +superclass is actually the way to go:

+ (id)someClassMethod {
    return [[[self superclass] superclass] someClassMethod];
}

since it is a class method, hence self refers to the class object where +someClassMethod is being defined.

On the other hand, things get a tad more complicated in instance methods. One solution is to get a pointer to the method implementation in the supersuper (grandparent) class. For instance:

- (id)someInstanceMethod {
    Class granny = [[self superclass] superclass];
    IMP grannyImp = class_getMethodImplementation(granny, _cmd);
    return grannyImp(self, _cmd);
}

Similarly to the class method example, +superclass is sent twice to obtain the supersuperclass. IMP is a pointer to a method, and we obtain an IMP to the method whose name is the same as the current one (-someInstaceMethod) but pointing to the implementation in the supersuperclass, and then call it. Note that you’d need to tweak this in case there are method arguments and return values different from id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...