designated initializer和secondary initializer是什么?

时间:2021-04-09 07:16:17

仅在此简单记录概念,方便以后回顾。。。

=====================================

designated initializer是指定初始化方法,提供所有参数;

secondary initializer间接初始化方法,可以有一个或多个,并且提供一个或

更多的默认参数来调用designated初始化的初始化方法。

designated initializer示例:

- (instancetype) initWithName:(NSString *)name
date:(NSDate *)birthday
age:(int)age
{
self = [super init];
if (self) {
_name = name;
_birthday = birthday;
_age = age;
}
return self;
}

secondary initializer示例:

- (instancetype)initWithName:(NSString *)name
{
return [self initWithName:name date:[NSDate date] age:];
} - (instancetype)initWithName:(NSString *)name date:(NSDate *)birthday
{
return [self initWithName:name date:birthday age:];
}