【问题标题】:KVC dot keys issueKVC点键问题
【发布时间】:2014-05-10 03:43:15
【问题描述】:

我创建类:

@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 也不起作用 - 无论如何都会崩溃。

【问题讨论】:

  • 你的标题中有#import KVOGame.h吗?
  • 你在哪里实例化 KVOPlatfrom 对象?它应该在 KVOGame 对象的实现中。你能粘贴那个实现的代码吗?
  • eharo2,就是这样!我没有创建 KVOPlatform 实例,这就是原因!你能用这个解决方案回答吗?我会检查一下。谢谢。

标签: ios objective-c cocoa kvc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多