开发者

Overloading init with variable param number

What is the proper way of overloading init methods with variable param number? I'm doing it:

- (id)initWithInt:(int)a
开发者_高级运维{
   return [self initWithInt:a andString:nil];
}


-(id)initWithInt:(int)a andString:(NSString*)str
{
    self = [super init];
    if (self) {
        NSLog(@"%@ %i",str,a);
    }

    return self;
}

It works, but the return [self initWithInt:a andString:nil]; does not seem right to me(there is no self at the moment, right?)


No, this is perfectly valid. Self is already setup, the idiom of reassigning self is for a couple of different edge cases (initialization fails, the initializer wants to return a different instance then the one you allocated, etc).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜