I still don't understand self=super init; ,although I read many many books and googles. I know that it is used for checking the current object equals to the return object from the parent class etc. However, I can not accept that is the answer. Can someone give me an easy but solid acceptable answer? Thanks!
The quick answer is that you are telling the super class of your class to initialise the object. Because you are inheriting from that super class, then you can perform other initialisation tasks specific to your implementation, but first you have to make sure the basics of initialisation are performed (which is handled by the base class NSObject). Each class that extends (inherits) from another class has to make sure their super class has been initialised before they can do their initialisation.
A very quickly made up (and therefore probably quite poor) analogue - before you could be created, your parents had to be created (the super class) and they had to have their parents created first (their super class)
I suggest you search for object oriented programming basics to learn about this sort of thing.
Your mention of "checking" makes me think that you are seeing =
as a comparison operation. It's not. It's an assignment. Sometimes the assignment appears within an if
condition. That's taking advantage of the fact that assignment expressions have a value -- the value that was assigned. So, if (self = [super init])
combines "call super's -init
method", "assign the result to self
", and "check if we got non-nil from super's -init
" (that is, that super didn't fail to initialize).
Because this combined assignment-and-test can be confusing and error-prone, Apple now recommends separating the assignment out from the check for failure:
self = [super init];
if (self)
// ... continue initializing ...
NSObject is root class. https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html You can find an init
method there. That init
method within NSObject
class is the initializer for every object in Objective-C. super
simply returns super class of a object.
If Class B
was inherited from Class A
, Class A
will be super class of Class B
. Again another class Class C
which was inherited from Class B
, the super class of Class C
will be Class B
. So on.
When you call [super init];
from a class, it simply call init
method of its super class, which in turn call init
method of its super class and so on. At last it will reach root object NSObject, and call the real init
method. At that point the iOS will allocate memory for your object and necessary data structures for your object. Read documentation about each method and property in NSObject class.
If you created a class MyView
which is subclass of UIView
, your class hierarchy will be.
NSObject
UIAppearanceContainer
UIAppearance
NSCoding
UIView
MyView
So when you call init
method in your class 'MyView', it will call
init method of UIView
UIView will call init method of NSCoding
NSCoding will call init method of UIAppearance
UIAppearance will call init method of UIAppearanceContainer
UIAppearanceContainer will call init method of NSObject.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.