So I am working my way through the Apple documentation of objective-C (before jumping into iphone development). One of the exercises states that I should create a designated
Declare and implement a new designated initializer used to create an XYZPerson using a specified first name, last name and date of birth...
You are correct in the declaration but your implementation is recursive, since it's calling itself. Do something like
//.h
-(id)initWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob;
//.m
-(id)initWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *)dob{
if(self = [super init]) {
// use the parameters to do something, eg.
_fName = fName; // assuming you have an ivar called _fName
_lName = lName; // assuming you have an ivar called _lName
_dob = dob; // assuming you have an ivar called _dob
}
return self;
}
Then
...along with a suitable class factory method.
A factory method is a class method that produces an instance of the object. The most common implementation is to have it to allocate and initialize a new instance of the object and return it.
//.h
+(instancetype)personWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob {
//.m
+(instancetype)personWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob {
return [[XYZPerson alloc] initWithNameAndDob:fName last:lName birth:dob];
}
Finally
Don’t forget to override init to call the designated initializer.
Since your designed initializer is initWithNameAndDob:last:birth:
your init
implementation must call it. The parameters of the designed initializer have to be a reasonable default, in this case nil
is fine.
-(id)init {
return [self initWithNameAndDob:nil last:nil birth:nil];
}
As a final remark I'd like to point out that your naming convention for the initializer is not that good. A more suitable and readable one would be
-(id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName dateOfBirth:(NSDate *) dob;