The Self and Super Keyword in IOS are important and one should know why and how they are used in objective c.
One should always access the instance variables directly from or within an initialization method. Because at the time when property is set or even if you do not provide accessor methods or you are aware of any side effects from within your own class, a future subclass will be perfectly override the behavior.
- (id)init {
self = [super init];
if (self) {
// initialize instance variables here
}
return self;
}
according to the above code-
Before doing its own initialization, an init method should assign self to the result of calling the superclasss initialization method. A superclass may fail to initialize the object correctly and return nil so you should always check to make sure that self is not nil before performing your own initialization.
By calling [super init] the first line in the method, an object is initialized from its root class down through each subclass init implementation in order.
- (id)initWithFirstName:(NSString *)aFirstName lastName:(NSString *)aLastName {
self = [super init];
if (self) {
_firstName = aFirstName;
_lastName = aLastName;
}
return self;
}
0 Comment(s)