此 class 与键 openKey 的键值编码不兼容。如何修复错误
This class is not key value coding-compliant for the key openKey. How to fix errors
错误来源。
@objc func selectHeader(sender: UIButton) -> Void {
var testSection = NSDictionary()
var openValue = String()
testSection = self.arrHeader.object(at: sender.tag) as! NSDictionary
openValue = testSection.value(forKey: self.isOpenKey) as! String
// value change
if openValue == "Y" {
testSection.setValue("N", forKey: self.isOpenKey) <—— App crash self.isOpenKey == “openKey”
} else {
testSection.setValue("Y", forKey: self.isOpenKey) <—— App crash self.isOpenKey == “openKey”
}
let section: NSIndexSet = NSIndexSet(index: (sender.tag))
self.collectionView.reloadSections(section as IndexSet)
}
TestSection.setValue 崩溃并出现以下错误。
2018-02-09 16:43:38.141[10730:2091077] * 由于未捕获的异常 'NSUnknownKeyException' 而终止应用程序,原因:'[<_TtGCs26_SwiftDeferredNSDictionarySSSS_ 0x1665dea0> setValue:forUndefinedKey:]:此 class 与键 openKey 的键值编码不兼容。'
* 首先抛出调用栈:
libc++abi.dylib:以 NSException
类型的未捕获异常终止
有什么问题?
您使用的 NSDictionary
有误。
首先-针对@Willeke 的评论,您不能修改NSDictionary
,您需要NSMutableDictionary
。你甚至可能想使用 Dictionary
.
那么,你应该调用setObject:forKey:
,而不是setValue:forKey:
我必须承认这个 API 可能有点令人困惑,因为它太像了:
你想要的是 map 对象的字典键条目,这是由 setObject:forKey:
完成的,在你的示例中(使用可变字典):testSection.setObject("N", forKey: "openKey")
。使用 Swift 方式和 Swift Dictionary
,你会写成 testSection["openKey"] = "N"
当使用 setValue:forKey:
时,您是 Key-Value-Coding,例如您正在调用 NSDictionary
实例的方法 ("sending a message"),消息的名称是您在键值中提供的名称。这将导致类似调用 textSection.openKey = "N"
的结果,因此出现异常
this class is not key value coding-compliant for the key openKey
在 NSMutableDictionary
的特殊情况下,如@matt 所述,只要您不提供 NSString
作为 setValue:forKey
和 setObject:forKey
的行为相同一个关键参数 - 在后一种情况下,将使用 KVC。
What is the problem?
"problem" 是你在 Swift 程序中使用 NSDictionary,而且你在整个程序中使用键值编码 (value(forKey:)
, setValue(_:forKey:)
)地方。停下来。这是 Swift,而不是 Objective-C。使用 Swift 类型和 Swift 方式与他们交谈。
错误来源。
@objc func selectHeader(sender: UIButton) -> Void {
var testSection = NSDictionary()
var openValue = String()
testSection = self.arrHeader.object(at: sender.tag) as! NSDictionary
openValue = testSection.value(forKey: self.isOpenKey) as! String
// value change
if openValue == "Y" {
testSection.setValue("N", forKey: self.isOpenKey) <—— App crash self.isOpenKey == “openKey”
} else {
testSection.setValue("Y", forKey: self.isOpenKey) <—— App crash self.isOpenKey == “openKey”
}
let section: NSIndexSet = NSIndexSet(index: (sender.tag))
self.collectionView.reloadSections(section as IndexSet)
}
TestSection.setValue 崩溃并出现以下错误。
2018-02-09 16:43:38.141[10730:2091077] * 由于未捕获的异常 'NSUnknownKeyException' 而终止应用程序,原因:'[<_TtGCs26_SwiftDeferredNSDictionarySSSS_ 0x1665dea0> setValue:forUndefinedKey:]:此 class 与键 openKey 的键值编码不兼容。' * 首先抛出调用栈: libc++abi.dylib:以 NSException
类型的未捕获异常终止有什么问题?
您使用的 NSDictionary
有误。
首先-针对@Willeke 的评论,您不能修改NSDictionary
,您需要NSMutableDictionary
。你甚至可能想使用 Dictionary
.
那么,你应该调用setObject:forKey:
,而不是setValue:forKey:
我必须承认这个 API 可能有点令人困惑,因为它太像了:
你想要的是 map 对象的字典键条目,这是由 setObject:forKey:
完成的,在你的示例中(使用可变字典):testSection.setObject("N", forKey: "openKey")
。使用 Swift 方式和 Swift Dictionary
,你会写成 testSection["openKey"] = "N"
当使用 setValue:forKey:
时,您是 Key-Value-Coding,例如您正在调用 NSDictionary
实例的方法 ("sending a message"),消息的名称是您在键值中提供的名称。这将导致类似调用 textSection.openKey = "N"
的结果,因此出现异常
this class is not key value coding-compliant for the key openKey
在 NSMutableDictionary
的特殊情况下,如@matt 所述,只要您不提供 NSString
作为 setValue:forKey
和 setObject:forKey
的行为相同一个关键参数 - 在后一种情况下,将使用 KVC。
What is the problem?
"problem" 是你在 Swift 程序中使用 NSDictionary,而且你在整个程序中使用键值编码 (value(forKey:)
, setValue(_:forKey:)
)地方。停下来。这是 Swift,而不是 Objective-C。使用 Swift 类型和 Swift 方式与他们交谈。