开发者

-(id)init using designated initialiser?

In the past I have always used method_1: but I have recently noticed a few instances where folks have instead called the superclasses designated initialiser. Does it matter, just curious if its more about style or substance?

Method_1:

-(id)init {
    self = [super init];
    if(self) {
        //do things ...
    }
    return self;
}

Method_2:

-(id)init {
    [super initWithNibName:nil bundle:nil];
    // do things ...
    return self;
}

-开发者_如何学运维(id)initWithNibName:(NSString *)nibName bundle:(NSString *)bundle {
    return [self init];
}

cheers Gary


Only a few instances?

The Apple documentation on Designated initialisers has this to say on subclassing:

General Principle: The designated initializer in a class must, through a message to super, invoke the designated initializer in a superclass.

Also:

Designated initializers are chained to each other through messages to super, while other initialization methods are chained to designated initializers through messages to self.

Your method 2 would be correct if:

  • -initWithNibName:bundle: is the designated initialiser of the super class
  • -init is the designated initialiser of your class (your example is a bit bizarre since you deliberately throw daway the nib name and bundle parameters)
  • -init was correctly coded not to throw away the return value from [super initWithNibName: bundle: ]

In your class you have to cover all of the super class init methods to ensure that your designated initialiser ultimately gets invoked.


Neither of the above make any sense for the init methed of a class who's super class has the designated initializer

 - (id)initWithNibName:(NSString *)nibName bundle:(NSString *)bundle

What are you trying to do?


The idea of a designated initialiser is that all other initialisation functions of a class should call it (this is only enforced by convention). This is nice as it means that someone writing a subclass can add additional steps to the initialisation while only overriding a single initialisor. When this pattern is being used, method 1 should only happen when init is the designated initialiser. One strategy for ensuring this occurs is as follows:

Suppose C inherits from B and let the designated initialisers be d_c and d_b. We override d_b in C to make it simply call d_c on itself. Since d_b is called by all other initialisers of B, this ensures that all initialisers present in the subclass call d_c. We then make all new initialisers call d_c too. d_c calls d_b in its superclass B, which will then pass the calls further up the chain.

Note that this strategy is the opposite of how classes are often initialised. For example, an initialiser with arguments a:1 b:2 could handle the b argument and then call another initialiser that could handle just the a argument. This works well when the function with arguments a and b is simple, by the designated initialiser works better in more complicated situations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜