Lee.zm
Lee.zm

Reputation: 105

ValueForKeyPath is not work how to fix it?

Animal class definition:

@property (nonatomic, strong) NSString *food;

Cat class definition:

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSData *birthday;
@property (nonatomic, strong) Animal *animal;

in main.m file:

[cat setValue:@"fish" forKeyPath:@"animal.food"];
NSLog(@"cat eat: %@", [cat valueForKeyPath:@"animal.food"]);

Result:

2016-01-16 19:31:33.811 Usage of KVC and KVO[6802:201576] cat eat: (null)

Why do I get null?

Upvotes: 0

Views: 74

Answers (1)

Lee.zm
Lee.zm

Reputation: 105

Thanks I've found the problem.add following code can be sovled. :)

[cat setValue:[[Animal alloc]init] forKeyPath:@"animal"];

Upvotes: 1

Related Questions