gpt4 book ai didi

objective-c - cocoa KVC : "class is not key value coding-compliant"

转载 作者:行者123 更新时间:2023-12-03 16:23:06 25 4
gpt4 key购买 nike

我正在尝试使用 KVC 更新一些属性。属性已合成。

这条线有效:

myObject.value = intValue;

这不起作用:

[self setValue:[NSNumber numberWithInt:intValue] forKey:@"myObject.value"];

并爆炸:由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]:此类与键 myObject.value 的键值编码不兼容.'

进一步,同一类的方法 (awakeFromNib) 的其他实例对 setValue:forKey: 调用响应良好。唯一的区别是这个特定实例是在 IB 中创建和连接的。

最佳答案

您不能将键路径作为第二个参数传递给 -[NSObject setValue:forKey:]。您想使用 setValue:forKeyPath: 代替:

[self setValue:[NSNumber numberWithInt:intValue] forKeyPath:@"myObject.value"];

我的理解是 setValue:forKey: 是作为性能优化而提供的。由于它不能采用 key 路径,因此它不必解析 key 字符串。

关于objective-c - cocoa KVC : "class is not key value coding-compliant",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1100101/

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