gpt4 book ai didi

ios - KVC 点键问题

转载 作者:行者123 更新时间:2023-11-28 20:02:01 24 4
gpt4 key购买 nike

我创建类:

@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]);
}

它因错误而崩溃:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key platform.platformVersion.'

我做错了什么?

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

最佳答案

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

我假设 game.platform 永远不会被创建。为零

关于ios - KVC 点键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23576871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com