动物类定义:
@property (nonatomic, strong) NSString *food;
Cat类定义:
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSData *birthday;
@property (nonatomic, strong) Animal *animal;
<.>在main.m文件中:
[cat setValue:@"fish" forKeyPath:@"animal.food"];
NSLog(@"cat eat: %@", [cat valueForKeyPath:@"animal.food"]);
结果:
2016-01-16 19:31:33.811 Usage of KVC and KVO[6802:201576] cat eat: (null)
为什么我会获得null
?
答案 0 :(得分:1)
由于 我发现了问题。可以解决以下代码问题。 :)
[cat setValue:[[Animal alloc]init] forKeyPath:@"animal"];