-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance
Asked Answered
C

2

13

Getting error when trying to utilize NSCoder

Player.swift:

class Player: NSObject, NSCoding {

    private var _playerName: String!
    private var _playerScore: Int!
    private var _playerColor: PlayerColor! //PlayerColor is an enum

    var playerName: String {
        get {
            return _playerName
        }
        set {
            _playerName = newValue
        }
    }

    var playerScore: Int {
        get {
            return _playerScore
        }
        set {
            _playerScore = newValue
        }
    }

    var playerColor: PlayerColor {
        get {
            return _playerColor
        }
        set {
            _playerColor = newValue
        }
    }

    init(playerName: String, playerScore: Int, playerColor: PlayerColor) {

        _playerName = playerName
        _playerScore = playerScore
        _playerColor = playerColor
    }

    required convenience init(coder aDecoder: NSCoder) {
        let name = aDecoder.decodeObject(forKey: "name") as! String
        let score = aDecoder.decodeInteger(forKey: "score")
        let color = aDecoder.decodeObject(forKey: "color") as! PlayerColor
        self.init(playerName: name, playerScore: score, playerColor: color)
    }

    func encode(with aCoder: NSCoder){
        aCoder.encode(playerName, forKey: "name")
        aCoder.encode(playerScore, forKey: "score")
        aCoder.encode(playerColor, forKey: "color")
    }

}

in PlayerStore.swift:

// Storage Functions
func savePlayers(){
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: _playerArray) // _playerarray is a [Player] the very object I want to store/retrieve at will
    defaults.set(encodedData, forKey: playerKeyForDefaults) //defaults is just var NSUserDefaults.standard
    defaults.synchronize()
}

func loadPlayers(){
    if let decoded = defaults.object(forKey: playerKeyForDefaults) as? NSData {
        let array = NSKeyedUnarchiver.unarchiveObject(with: decoded as Data) as! [Player]
        _playerArray = array
    }

}
Canvass answered 5/10, 2016 at 17:39 Comment(2)
You cannot directly encode enums, compare https://mcmap.net/q/906577/-swift-enum-and-nscoding - duplicate?Katabasis
Btw, your getters, setters and underscore variables are not needed in Swift (as in some other languages). Unless you have special needs, it is just var playerName: String etc.Katabasis
C
9

Here's the solution I implemented:

Player.swift:

import Foundation

    class Player: NSObject, NSCoding {

        private var name: String!
        private var score: Int!
        private var color: String!

        var playerName: String {
            get {
                return name
            }
            set {
                name = newValue
            }
        }

        var playerScore: Int {
            get {
                return score
            }
            set {
                score = newValue
            }
        }

        var playerColor: String {
            get {
                return color
            }
            set {
                color = newValue
            }
        }

        init(playerName: String, playerScore: Int, playerColor: String) {

            name = playerName
            score = playerScore
            color = playerColor
        }

        required convenience init(coder aDecoder: NSCoder) {
            let name = aDecoder.decodeObject(forKey: "name") as! String
            let score = aDecoder.decodeObject(forKey: "score") as! Int
            let color = aDecoder.decodeObject(forKey: "color") as! String
            self.init(playerName: name, playerScore: score, playerColor: color)
        }

        func encode(with aCoder: NSCoder){
            aCoder.encode(name, forKey: "name")
            aCoder.encode(score, forKey: "score")
            aCoder.encode(color, forKey: "color")
        }

    }

PlayerStore.swift:

func savePlayers(){
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: _playerArray)
    defaults.set(encodedData, forKey: playerKeyForDefaults)
}

func loadPlayers(){
    if let decoded = defaults.object(forKey: playerKeyForDefaults) as? NSData {
        let array = NSKeyedUnarchiver.unarchiveObject(with: decoded as Data) as! [Player]
        _playerArray = array
    }

}
Canvass answered 8/10, 2016 at 3:6 Comment(2)
I have two browsers up with a link to this post. I'm trying to figure out what is different? Also, why did whatever change you make help?Zenas
@Zenas OP removed the custom PlayerColor-enum and changed it with a String. As commented above, enums can't be encoded.Eleventh
S
6

You can also use this approach using Enum rawValue. It might help you to archivedData and unarchiveObject a complete model object.

Color Enum With Hex value:

enum PlayerColor: String {
    case red = "#FF0000"
    case silver = "#C0C0C0"
    case gray = "#808080"
    case black = "#000000"

    var description: String {
        return self.rawValue
    }
}

 import Foundation

class Player: NSObject, NSCoding {

    private var name: String!
    private var score: Int!
    private var color: PlayerColor!

    init(playerName: String, playerScore: Int, playerColor: PlayerColor) {

        name = playerName
        score = playerScore
        color = playerColor
    }

    required convenience init(coder aDecoder: NSCoder) {
        let name = aDecoder.decodeObject(forKey: "name") as! String
        let score = aDecoder.decodeObject(forKey: "score") as! Int

        if let value = aDecoder.decodeObject(forKey: "color") as? String{
            color = PlayerColor(rawValue: value)
        }
        self.init(playerName: name, playerScore: score, playerColor: color)
    }

    func encode(with aCoder: NSCoder){

        aCoder.encode(name, forKey: "name")
        aCoder.encode(score, forKey: "score")

        let value = color!.description
        aCoder.encode(value, forKey: "color")
    }

}
Signesignet answered 24/10, 2019 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.