IOS Self and Super Learning

Source: Internet
Author: User

Someone asked me the wrong answer to the question, which is probably saying that [self class] and [Super class] Print the result is not the same.

I opened my eyes and said it was different. Because I remember doing it a couple of days ago. DFS gets the reflection base class used to this piece

Then Pia Pia faces

Run through the answers first:

Inside the Appdelegate:

NSLog (@ "%@%@%@", [Self class],[super class],[self superclass]);

Printing results:

2016-05-18 16:38:14.918 dailylife[34114:1143377] appdelegate appdelegate uiresponder

Actually, I can see the difference.

Like the imaginary, "[self superclass]" presumably thinks that the end result is the same person who thinks [super class] is [self superclass].

In fact, in the development process I really did not say because the problem is wrong.

Because the problem of override (reuse) is first considered in writing the Init method, [Super Class] is written first. Then the reuse you want to implement is written below the line code.

If I had considered this practical situation then I would not have answered the wrong question, really ashamed.

This is the pure theory and actual combat for a different interpretation of the problem.

So now we'll talk about why [self class] and [Super class] print the same results.

Self: My understanding is that the object of the current class is itself, and that is understood to be the class that gets the current object. English explanation: "Self refers to the object receiving a message in objective-c programming."

Super : Online explanation It is a compiler instruction symbol, which I personally understand now is a system-level backtracking lookup that always finds the root, and the recipient of the return is the [self class]; English explanation: "Super is a flag that tells T He compiler to search for the method implementation in a very different place. It begins in the superclass of the class that defines the method where super appears. "

The underlying mechanism has been deliberately spoken on the Internet: I'm not going to post code.

http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective[nil]c-runtime%281%29[nil]-self-and-super/

But I don't think the internet is saying or asking for something like "Why does," [self class] = = [Super class]? "Or they are two equal." I can only say that their results are consistent to some extent replaceable.

But abuse also has consequences, and there is a closed loop.

-(instancetype)initwithframe:(cgrect) frame{self    = [Super  initWithFrame:frame];    if (self) {     //todo:            }    return to self;}
-(Instancetype)initwithframe:(cgrect) frame{self    = [selfinitwithframe: Frame];    if (self) {     //todo:            }    return to self;}
-(instancetype)initwithnew:(cgrect) frame{    initwithframe: Frame];//super as well    if (self {     //todo:            } return self    ;}

It should be easy to see if I write this.

In the first case, the super-compilation instruction starts backtracking, if there is a rewrite backtracking rewrite and then continues backtracking until the base class.

The second piece of code calls the current method again and then falls into a dead loop. (similar to this situation can not be abused, and we know in the initialization method as far as possible not to use the DOT syntax, not necessarily when the circular reference, of course, we know that there is this hole, we have to avoid the mechanism also does not matter)

The third piece of code we see this time the method name is different initwithnew: and initwithframe: , so with super Self is OK, self also eventually go to Super method. You can also work Process to write these methods and then click In, or debug a look at each step.

Learn to what degree will have not understand the question, perhaps to others very simple, oneself will. It doesn't matter if you dare to find your face.

Accumulate slowly, and the days are long.

IOS Self and Super Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.