objective-c - calling one constructor from another
- by synic
Say you had the following two constructors:
- (id)initWithTitle:(NSString *)title;
- (id)initWithTitle:(NSString *)title page:(NSString *)page;
The second constructor is no different from the first, except that it sets up the member variable "page".
Since it basically has to do the same thing, is there a way to call the first one from the second one to reduce code duplication, or do you have to set up a third method to do the common tasks?
I'm talking about something similar to this, though I doubt this will work:
- (id)initWithTitle:(NSString *)_title {
if(self = [super init]) {
self.title = _title;
}
return self;
}
- (id)initWithTitle:(NSString *)_title page:(NSString *)_page {
if(self = [self initWithTitle:_title]) {
self.page = _page;
}
return self;
}