티스토리 뷰

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];'.


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함