It seems that I should implement three initializers. A main initializer is the one that calls [super init] and all the variables are assigned in this method. But since other two initializers have non-overlapping arguments both of them calls the main initializer.
Is this approach correct ? In many examples a third level initializer call the second lavel and then the second level calls the first one while here both the first and the second level directly calls the first level initializer.
-(id)initWithAccount:(NSString *)account_name apiKey:(NSString *)api_key lineid:(NSString *)line_id runTitle:(NSString *)run_title data:(NSString *)stringData runID:(NSString *)run_id{
if(self = [super init]){
accountName = [account_name retain];
runID = [run_id retain];
apiKey = [api_key retain];
lineID = [line_id retain];
data = [stringData retain];
runTitle = [run_title retain];
}
return self;
}
-(id)initWithAccount:(NSString *)account_name apiKey:(NSString *)api_key runID:(NSString *)run_id{
return [self initWithAccount:account_name apiKey:api_key lineid:nil runTitle:nil data:nil runID:run_id];
}
-(id)initWithAccount:(NSString *)account_name apiKey:(NSString *)api_key lineID:(NSString *)line_id{
return [self initWithAccount:account_name apiKey:api_key lineid:line_id runTitle:nil data:nil runID:nil];
}
To some extent it’s a matter of taste whether an initializer calls the designated initializer directly or through another initializer on the class. The important thing is, that the designated initializer gets called in any case.
In your example you’re calling super with a comparison operator (==) instead of assigning it (=). You need to change this:
to either this:
or this: