Unfortunately I am not able to add a list of my own class objects to UserDefaults
. The following error is generated:
NSForwarding: warning: object 0x6080002502c0 of class 'ClrLearn.highscoreStructure' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[ClrLearn.highscoreStructure >replacementObjectForKeyedArchiver:]
The class looks as follows (it has been modified according to the various topics on the stack for example that one - how can store custom objects in NSUserDefaults):
class highscoreStructure {
var name : String = ""
var score : Int = 0
init(name: String, score: Int) {
self.name = name
self.score = score
}
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as? String ?? ""
self.score = decoder.decodeInteger(forKey: "score")
}
func encode(with coder: NSCoder) {
coder.encode(name, forKey: "name")
coder.encode(score, forKey: "score")
}
}
Ok, feels like I made something wrong to the Stack rules, so sorry rmaddy - it was first and last time, I prommise. :)
But going back to the problem, first part was solved by vadian - thanks a lot! But still that part of my app not work: I've set rootObject (NSKeyedArchiver.archivedData(withRootObject: highscoreStructObjects)) as my array of objects (so stupid mistake!) but still have errors like that:
[ClrLearn.HighscoreStructure encodeWithCoder:]: unrecognized selector sent >to instance 0x6080002586c0
or
Terminating app due to uncaught exception 'NSInvalidArgumentException', >reason: '-[ClrLearn.HighscoreStructure >encodeWithCoder:]: unrecognized >selector sent to instance >0x6080002586c0' –
Ps. I'm not sure is it the place where I should error - debug log is still not clear at all to me, at least not clean as one in Visual Studio. :) Maybe I should paste something other?
Pps. This line of code looks like:
let encodedData = NSKeyedArchiver.archivedData(withRootObject: highscoreStructObjects)
UserDefaults.standard.set(encodedData, forKey: "highscores")