29,028
社区成员
发帖
与我相关
我的任务
分享
//解决问题,怎么通过类方法初始化一个readonly属性?
//头文件中并未声明这个方法,只能在里面使用。
//这个方法的作用是为了给下面一个类方法的使用。(因为在类方法中无法使用属性和实例方法,但是可以使用self)
- (instancetype)initWithButtonType:(UIButtonType)buttonType {
self = [super init];
_buttonType = buttonType;
return self;
}
+ (instancetype)buttonWithType:(UIButtonType)buttonType{
//这里相当于生成一个对象
return [[self alloc] initWithButtonType:buttonType];
}