Why does initWithCoder not return instancetype?

删除回忆录丶 提交于 2019-12-12 00:32:35

问题


It seems that most init methods in Objective-C now tend to return instancetype instead of id. See [UIView initWithFrame:], [UIViewController initWithNibName:bundle:], [NSArray init] and siblings, etc. But initWithCoder uses id. Why is this? Has it just not been updated yet? Or is there a reason it has to be id?


回答1:


It is not updated yet. You can still code it with instance type.

     - (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        //...
    }
    return self;
}


来源:https://stackoverflow.com/questions/32143209/why-does-initwithcoder-not-return-instancetype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!