在objective-c中,if(self){[self initFOO]}是多余的?

Wil*_*ken 1 objective-c

我正在从Paul Hegarty的Stanford CS193P学习.然后我看到这样的东西,我也看到其他人使用:

- (id)init
{
    self = [super init];
    if (self) {
        [self someSetupFunctions];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

这不是多余的吗?为什么不使用:

- (id)init
{
    self = [super init];
    [self someSetupFunctions];
    return self;
}
Run Code Online (Sandbox Code Playgroud)

我认为消息只是失败,所以为什么不使用这第二种格式?

sch*_*sch 5

除了将消息传递给self之外,您可能还想做其他事情.例如:

- (id)init
{
    self = [super init];
    if (self) {
        // set up ivars
        _anIvar = 5;
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)