objective-c - calling one constructor from another
Posted
by synic
on Stack Overflow
See other posts from Stack Overflow
or by synic
Published on 2010-05-15T14:10:39Z
Indexed on
2010/05/15
14:14 UTC
Read the original article
Hit count: 144
objective-c
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;
}
© Stack Overflow or respective owner