开发者

InitWithCoder, [super init] or [super initWithCoder]?

开发者 https://www.devze.com 2023-03-22 15:32 出处:网络
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\".

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".

// A
- (id)initWithCoder:(NSCoder *)decoder {
    self=[supe开发者_JS百科r 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. If it does, you must call [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.


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

[super initWithCoder:decoder]

Otherwise call its designated initializer.

0

精彩评论

暂无评论...
验证码 换一张
取 消