Reputation: 765
I create class:
@interface KVOGame : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *releaseDate;
@property (nonatomic, strong) KVOPlatform *platform;
@end
And another one for it property:
@interface KVOPlatform : NSObject
@property (nonatomic, strong) NSString *platformName;
@property (nonatomic, strong) NSString *platformVersion;
@end
Then I try to use KVC to get "platformVersion" like this
@interface KVOViewController ()
@end
@implementation KVOViewController
- (void)viewDidLoad
{
[super viewDidLoad];
KVOGame *game = [[KVOGame alloc] init];
game.name = @"Bayonetta";
game.releaseDate = @"8.10.2009";
game.platform.platformName = @"PS3, Xbox360";
game.platform.platformVersion = @"all versions";
NSString *identifier = @"platform.platformVersion";
NSLog(@"%@", [game valueForKey:identifier]);
}
And it crash with error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key platform.platformVersion.'
What am I doing wrong?
Update: valueForKeyPath:
instead of valueForKey
also does not work - crash anyway.
Upvotes: 0
Views: 114
Reputation: 52227
try NSLog(@"%@", [game valueForKeyPath:identifier]);
I assume game.platform
never to be created. It is nil
Upvotes: 3