0

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")
Janoz
  • 33
  • 1
  • 8

2 Answers2

2

To be able to implement NSCoding the class must inherit from NSObject.

class HighscoreStructure : NSObject { ...

By the way, class names are supposed to start with a capital letter.

And decodeObject(forKey: "name") can never be nil you can safely write

self.name = decoder.decodeObject(forKey: "name") as! String
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Ok, I hope that this time I wont't make any mistake - the problem was solved by rmaddy in other "topic", but vadian was very, but very close - to implement NSCoding I need to inherit from NSObject as he wrote but also from... NSCoding! Isn't it obvious? For me it wasn't... In the other hand maybe he tried to tell me that I should inherit from both but my english was to bad to get it. Anyway I found an answer so thank You very much Vadian, Rmaddy and sorry one more time for breaking some kinds of SOF rules... It was first and last time! Oh and there is a thread when I finally find the answer, and yeap I was blind that I missed it earlier - encodeWithCoder: unrecognized selector sent to instance

Janoz
  • 33
  • 1
  • 8