0

我创建类:

@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:]:此类不符合关键平台.平台版本的关键值编码。”

我究竟做错了什么?

更新:valueForKeyPath:而不是valueForKey也不起作用 - 无论如何都会崩溃。

4

1 回答 1

3

尝试NSLog(@"%@", [game valueForKeyPath:identifier]);

我假设game.platform永远不会被创造。它是零

于 2014-05-10T03:49:30.447 回答