this happens to be in the context of UserDefaults. I'm trying to track down an issue where a particular class is turned into it's superclass, either when saving to UserDefaults, or loading from UserDefaults.
the classes in question (the super class and the class that gets 'permanently coerced') are for all purposes have the same footprint in storage. Same properties. Just different behavior after being loaded. What is saved to UserDefaults is a Data object constructed from an array.
the array is defined as an array of the superclass type:
var items : [BKLayerController]
the saving and the loading code:
open var items : [BKLayerController] {
get{
var theObjs : [BKLayerController] = []
if let theData = UserDefaults.standard.data(forKey: BKToolboxDefaults) as? Data{
do {
theObjs = try PropertyListDecoder().decode([BKLayerController].self, from: theData)
} catch {
debugPrint(error)
}
}
return theObjs
}
set{
do {
let theData = try PropertyListEncoder().encode(newValue)
UserDefaults.standard.set(theData, forKey: BKToolboxDefaults)
} catch {
debugPrint(error)
}
}
}
the Defaults registration code:
let layer = BKLayerController()
layer.name = "layer"
let textLayer = BKTextLayerCon()
textLayer.name = "textLayer"
do {
let theData = try PropertyListEncoder().encode([layer,textLayer])
UserDefaults.standard.register(defaults: [BKToolboxDefaults : theData ])
} catch {
}
the class "BKTextLayerCon"is a "BKLayerController" class (it's super class) upon loading of the Defaults. It may happen during the saving process, I still haven't figured out how to test that yet.
my question, is that when you load objects with the propertyListDecoder (and with Codable Decoders in general) do you need to specify specific types? If so, what would that look like? like this?
if let theData = UserDefaults.standard.data(forKey: BKToolboxDefaults) as? Data{
do {
theObjs = try PropertyListDecoder().decode([[BKLayerController].self, [BKTextLayerCon].self], from: theData)
} catch {
debugPrint(error)
}
}