我创建类:
@interface KVOGame : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *releaseDate;
@property (nonatomic, strong) KVOPlatform *platform;
@end
另一个属性:
@interface KVOPlatform : NSObject
@property (nonatomic, strong) NSString *platformName;
@property (nonatomic, strong) NSString *platformVersion;
@end
然后我尝试使用KVC来获取“ platformVersion”,如下所示
@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]);
}
它崩溃并显示错误:
由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类与密钥platform.platformVersion的编码都不兼容。
我究竟做错了什么?
更新:
valueForKeyPath:
而不是valueForKey
也不起作用-仍然崩溃。 最佳答案
尝试NSLog(@"%@", [game valueForKeyPath:identifier]);
我假设game.platform
永远不会被创建。零
关于ios - KVC点键问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23576871/