designated initializer和secondary initializer是什么?

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

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

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:0];
}

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

 

posted @ 2016-12-06 10:27  XA_Fanny  阅读(499)  评论(0编辑  收藏  举报