티스토리 뷰

I think you will agree with this following sentence.


If you want to create new initialiser with some parameter, please don't forget to call '[super init];'.


For example,


- (instancetype)initWithString:(NSString *)string

{

self = [super init];

if (self) {

self.text = string;

}

return self;

}


And now, do you agree with the written code? May be you found already the problem.


In this case, I preffer to call '[self init];'' like following code.


- (instancetype)initWithString:(NSString *)string

{

self = [self init];

if (self) {

self.text = string;

}

return self;

}


because if there can be some initialising in 'init'.

This code will guarantee that your code will not miss some initialising process for this interface.


Someone can make some argument.


"If super class has already that method, you have to call '[super initWithString:];'"


Yes totally right. and in that initialiser of super class, it has to call '[self init];'.


댓글
댓글쓰기 폼