I want to share with you a question about super and superclass, which is also a common interview question. Please see the code below:

What is the current output of the 4 results? We probably don’t write code like this, so the interviewer asks you these questions to see if you understand super and superclass thoroughly, so let’s go into that.

First we must know that [self class] must be the current class, GDStudent; And [self SuperClass] is definitely its parent, GDPerson

These two should be in no doubt, let’s see if this is the case, see the code below:

These two are pretty obvious answers, so let’s see how super works, so let’s just write a random method in GDStudent like -(void)test;

The.m file plus the implementation is shown below:

As long as we understand how test is called, we will know what super does! So as usual, let’s convert gdstudent.m into the underlying c++ file to see what the implementation looks like. Once you figure out what [super test] does, the rest of the problem takes care of itself. xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc GDStudent.m -o GDStudent.m-arm64.cpp)

So let’s just focus on the code in the green box, and let’s simplify it a little bit so we can see it a little bit more clearly.

Above is a simplified version of the source code, is the same effect, here [super Test] is an arg that sends a class_getSuperclass message to @selector(test). Then we’ll look at the source code for _objc_super See what’s going on inside the structure! The following figure

__rw_objc_super source code implementation

__rw_objc_super we can simplify again. We are getting closer to the answer, see the simplified picture below:

Self: the receiver above; Class_getSuperclass (objc_getClass(“GDStudent”) : super_class above.

Objc_msgSendSuper objc_msgSendSuper objc_msgSendSuper objc_msgSendSuper

The first parameter is the message receiver (self). The first parameter is the message receiver (self). The second is the place to start looking for methods.

To put it bluntly, when calling [super Run] to start looking for a run method, you first start looking for the GDPerson of the parent class! It should be clear that the message receiver is self(GDStudent);

conclusion

When the super method is called, the method is first looked up from the class object of the parent class, and the message receiver is still the current class.

Ok, knowing that, let’s move on to the interview questions, and guess what these two answers are?

// NSLog(@”%@”,[super class]);

// NSLog(@”%@”,[super superclass]);

Let’s take a look at the results:

Results the operation

Is there a very surprised students. First of all, where is the class method? For an OC object, there’s a class method, right, so the class method is in NSObject, so there should be no problem here.

Now let’s go to objC source code and take a look at the underlying implementation of class and superclass, as shown below:

So at this point, it’s clear why the above results occur:

NSLog(@”%@”,[self class]);

NSLog(@”%@”,[self superclass]);

NSLog(@”%@”,[super class]);

NSLog(@”%@”,[super superclass]);

They are current class, parent class of current class, current class, and parent class of current class.

Conclusion [super message]

1. Message receivers are still subclass objects

2. Start with the parent class to find the implementation of the method.

In the next blog I will introduce the difference between isKindOfClass and isMemberOfClass.

For example, do you know the value of the following result?

BOOL res1 = [[NSObject class] isKindOfClass:[NSObject class]];

BOOL res2 = [[NSObject class]isMemberOfClass:[NSObject class]];

BOOL res3 = [[GDStudent class]isKindOfClass:[GDStudent class]];

BOOL res4 = [[GDStudent class]isMemberOfClass:[GDStudent class]];

After this interview question, we’ll move on to other topics about Runtime to continue learning about Runtime

If you find my writing helpful, please follow me and I will continue to update 😄