给model赋值一直为nil

自定义三个model类分别为OneModel、TwoModel、AllModel
OneModel.h为

@property(nonatomic ,copy)NSString *oneName;

TwoModel.h为

@property(nonatomic ,copy)NSString *twoName;

AllModel.h为

@property(nonatomic ,strong)OneModel *oneModel;
@property(nonatomic ,strong)TwoModel *twoModel;

接下来要对AllModel的实例化对象赋值

AllModel *model = [AllModel new];
if(model.oneModel.oneName == nil){
        model.oneModel.oneName = @"123";
}else{
        [model.oneModel setValue:@"abc" forKey:@"oneName"];
}

model.twoModel.twoName = @"789";
[model.twoModel setValue:@"xyz" forKey:@"twoName"];

以上对model赋值全为nil。
原因,没有给对象初始化。如果要进行赋值:

AllModel *model = [AllModel new];
OneModel *model1 = [OneModel new];
TwoModel *model2 = [TwoModel new];

model1.oneName = @"123"; 
model2.twoName = @"789";

model.oneModel = model1;
model.twoModel = model2;

你可能感兴趣的:(给model赋值一直为nil)