InitWithCoder,[super init]还是[super initWithCoder]?

[英]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) {
        ...

2 个解决方案

#1


12  

If the superclass adopts NSCoding (which is likely albeit not always the case), always call

如果超类采用NSCoding(可能并非总是如此),请始终调用

[super initWithCoder:decoder]

Otherwise call its designated initializer.

否则调用其指定的初始化程序。

#2


13  

Depends on whether the superclass conforms to the NSCoding protocol or not. If it does, you must call [super initWithCoder:decoder]. If it does not, you must call the superclass' designated initializer.

取决于超类是否符合NSCoding协议。如果是,则必须调用[super initWithCoder:decoder]。如果没有,则必须调用超类的指定初始值设定项。

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协议。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/07/25/ec9d2cb1960dc72914d956846225cec9.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号