我有一堂课:
@interface BasicDescription : NSObject
@property (strong,nonatomic) NSString *author;
@property (assign,nonatomic) NSInteger year;
@property (strong,nonatomic) NSString *owner;
第二类:
@interface AdvDescModel : BasicDescription
-(id)initWithBasicModel:(BasicDescription*)basicModel;
所以我的问题是当我创建新对象时如何分配所有属性。我现在有
AdvDescModel *adv = [AdvDescModel alloc] initWithBasicModel:basic];
然后在初始化程序中:
-(id)initWithBasicModel:(BasicDescription*)basicModel {
if(self = [super init]) {
[self setAllParams:basicModel];
}
return self;
}
-(void)setAllParams:(BasicDescription*)bModel {
[self setAuthor:bModel.author];
[self setYear:bModel.year];
[self setOwner:bModel.owner];
}
但是有没有更好的方法来做到这一点?现在我必须查看我所有的属性并为每个属性调用 setter。