开发者

Overloading init with variable param number

开发者 https://www.devze.com 2023-03-31 05:54 出处:网络
What is the proper way of overloading init methods with variable param number? I\'m doing it: - (id)initWithInt:(int)a

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

精彩评论

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