Open BusyBoBo opened 4 years ago
Hey @BusyBoBo, because you assign the values it makes your struct always initialize with given values. I'd suggest making it a default values for initializer so you can allow Codable to actually get the values from the data:
struct UserConfig: Codable, DefaultsSerializable {
var isFirstLaunch: Bool
var country: String
var sortType: SCSortType
var picQuality: SCPicQuality
var launchType: SCLaunchType
var layout: SCLayoutType
var sysConfig: SysConfig
init(isFirstLaunch: Bool = true, country: String = "", sortType: SCSortType = .timeDESC, picQuality: SCPicQuality = .high, launchType: SCLaunchType = .document, layout: SCLayoutType = .collection, sysConfig: SysConfig = SysConfig()) {
self.isFirstLaunch = isFirstLaunch
....
}
}
This should help with getting both the default values on initializer but also being able to fetch the values from the encoded data.
I've tried and still not work
Implement init(from decoder: Decoder)
in UserConfig
to override the default decoding behavior.
This is my code
When I added
sysConfig
, all the values were restored to the default values.Where did I write wrong and how should I modify it? Thanks.