1

I'm new in Swift, and i have an issue with error this class is not key value coding-compliant for the key.

I have reread all related topics and did not find a solution to this problem.

Please check out the code and give me some advice, what have I done wrong?

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)

The error:

<__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
Rurom
  • 253
  • 3
  • 18
  • 2
    Essentially a duplicate of https://stackoverflow.com/questions/26366082/cannot-access-property-of-swift-type-from-objective-c: `Int?` is not representable in Objective-C. Make it an `Int` or an `NSNumber?` – Martin R Jul 18 '17 at 19:58
  • @Martin R Thank you for the help! – Rurom Jul 18 '17 at 20:11
  • I hit this too.. Unfortunately the “lessoned learned” is that Swift is not nearly as flexible and powerful as Objective-C this is a no-brainer in the native language – Roy Lovejoy Jan 17 '20 at 23:56

1 Answers1

5

As far as I can tell, your example has a number of errors. First, you define var dataArr, then redefine let dataArr, then actually iterate over dataArray.

That aside, your dataArr is an array of dictionaries. Each dictionary is of type [String: Any]. When you call setValuesForKeys(dictionary), it is attempting to set the value for name and highestScore to the values in the dictionary. Each of those values is of type Any, but what the FriendScore class expects is a value of type String for name, and a value of type Int for highestScore. You will need to cast each of the values to the correct type:

class FriendScore: NSObject {
    var name:String?
    var highestScore:Int?
}

let dataArr = [["name": "Ben", "highestScore": 15],["name": "Alex", "highestScore": 12]]

for user in dataArr {
    let friendScore = FriendScore()
    friendScore.name = user["name"] as? String
    friendScore.highestScore = user["highestScore"] as? Int
}

If your class had several parameters of the same type, you could simply use setValuesForKeys and pass a dictionary with an explicit type other than Any.

drootang
  • 2,351
  • 1
  • 20
  • 30
  • "var dataArr, then redefine let dataArr" - this is just an error in the example, typo, i missspell it unfortunatly. The key problem was in Friendscore class, Martin R already helped me. But anyway, @drootang Thanks for the analysis and for your advices and comments. I appreciate it! – Rurom Jul 19 '17 at 06:48