我是Swift中的新成員,並且我有一個錯誤問題此類不是密鑰編碼兼容的密鑰。Swift - setValuesForKeys(dictionary)錯誤 - 這個類不是關鍵值編碼兼容關鍵
我已重讀所有相關主題,但未找到解決此問題的方法。
請查看代碼並給我一些建議,我做了什麼錯誤?
class FriendScore: NSObject {
var name:String?
var highestScore:Int?
}
var allScoresArr = [FriendScore]()
var dataArr = [[String:Any]]()
dataArr =[["name": "Ben", "highestScore": 15],["name": "Alex", "highestScore": 12]]
for user in dataArray {
if let dictionary = user as? [String:Any] {
let friendScore = FriendScore()
//Error Happens Here "Thread Breakpoint"
friendScore.setValuesForKeys(dictionary)
allScoresArr.append(friendScore)
}
}
print(allScoresArr)
錯誤:
<__lldb_expr_73.FriendScore 0x608000266080>
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key highestScore.
'libc++abi.dylib: terminating with uncaught exception of type NSException
本質上是一個重複的https://stackoverflow.com/questions/26366082/cannot-access-property-of-swift-type-from-objective-c:'Int?'不能在Objective-C中表示。讓它成爲'Int'還是'NSNumber?' –
@Martin R感謝您的幫助! – Rurom