I would like to know how can I encode an enum made of Hashables? I know how to encode it using Codable, but in my case it has to be done by using NSCoding. My enums are as following:
enum ItemCatalog: Hashable {
case sword(Swords)
case shield(Shields)
case armor(Armors)
}
enum Swords: String{
case sabre
case xiphos
case broadsword
}
enum Shields: String {
case buckler
case heaterShield
}
enum Armors: String {
case studArmor
case chainmail
case plateArmor
}
I tried to encode it by:
override func encode(with aCoder: NSCoder) {
aCoder.encode(catalogCode, forKey: "catalogCode")
}
and the app crashed with an error, therefore, I tried:
aCoder.encode(catalogCode.hashValue, forKey: "catalogCode")
the app will run without crashing but I'm not sure if it was the right way to encode it. Anyhow, when it comes to decoding it, I have no idea how to do it. I tried the following and the app crashed because it returned a nil:
required convenience init?(coder aDecoder: NSCoder) {
let catalogCode = aDecoder.decodeObject(forKey: "catalogCode") as? ItemCatalog
self.init(itemCatalogCode: catalogCode!)
}
Could someone please show me how to do it?
Thanks in advance for any help.