[英]InitWithCoder, [super init] or [super initWithCoder]?
Can I ask which version I should be using, in my older apps I seem to be using "B" but when I look at a lot of examples online I am seeing a lot of versions that look like "A". 我可以问我应该使用哪个版本,在我的旧应用程序中,我似乎使用“B”但是当我在网上查看很多示例时,我看到很多版本看起来像“A”。
// A
- (id)initWithCoder:(NSCoder *)decoder {
self=[super initWithCoder:decoder];
if(self) {
...
OR 要么
// B
- (id)initWithCoder:(NSCoder *)decoder {
self=[super init];
if(self) {
...
Depends on whether the superclass conforms to the NSCoding
protocol or not. 取决于超类是否符合
NSCoding
协议。 If it does, you must call [super initWithCoder:decoder]
. 如果是,则必须调用
[super initWithCoder:decoder]
。 If it does not, you must call the superclass' designated initializer. 如果没有,则必须调用超类的指定初始化程序。
For example, if your class is a direct subclass of NSObject
, you would call [super init]
, NSObject
's designated initializer, since NSObject
does not conform to the NSCoding
protocol. 例如,如果您的类是
NSObject
的直接子类,那么您将调用[super init]
, NSObject
的指定初始化程序,因为NSObject
不符合NSCoding
协议。
If the superclass adopts NSCoding
(which is likely albeit not always the case), always call 如果超类采用
NSCoding
(可能并非总是如此),请始终调用
[super initWithCoder:decoder]
Otherwise call its designated initializer. 否则调用其指定的初始化程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.