简体   繁体   中英

NSCoder, NSObject vs UIView

When we implement (id)initWithCoder:(NSCoder *)decoder method in NSObject class, we need to call [super **init**] , but in UIView somehow we need to call [super **initWithCoder:decoder**] . Why is there such a difference? The question is not a duplicate because it concerns protocols conformance to NSCoder in this particular case. There can well be other classes' situation similar to the one specified in the question.

You should call [super initWithCoder:decoder]; for any class that subclasses a class that already conforms to the NSCoding protocol.

In the case of a class that directly extends NSObject then of course you can't call [super initWithCoder:decoder]; because NSObject doesn't conform to NSCoder and therefore doesn't have an initWithCoder: you can call from the subclass. So you must call some other init method.

In the case of a custom view extending UIView , you do call it because UIView conforms to NSCoding .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM