Why not throw an exception if [super init] returns nil?

后端 未结 4 2402
北海茫月
北海茫月 2021-02-19 18:15

This is considered typical

- (id)init {
    self = [super init];
    if (self) {
        // <#initializations#>
    }
    return self;
}

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-19 19:05

    One reason, why you should do, what JustSid is saying:

    In object-orientated design you should always code, as if you maybe will hand your class over to another project by another developer. So you can't assume, that a failure in initialization in his project may be as bad as it is probably in yours. Maybe this developer is you in 5 years. Imagine your hassle to fix your 200 classes, you want to reuse.

提交回复
热议问题