Given is the following enum:
enum TimerType: TimeInterval, Codable {
case timer, `break`
var rawValue: TimeInterval {
switch self {
case .timer: return 60 * 25
case .break: return 60 * 5
}
}
enum CodingKeys: String, CodingKey {
case timer = "timer"
case `break` = "break"
}
}
I want save its values in structs that are using this enum to json like this:
{
"type": "timer"
}
But what it actually does is
{
"type": 1500
}
While I can see that it actually saves the Double
value (as it's of type TimerInterval
which is a typealias of Double), I cannot figure out how to encode and decode with their names. Any hints?
Since you have the timing values hardcoded, I would suggest switching to a String-based enum:
enum TimerType: String, Codable {
case timer, `break`
var timerValue: TimeInterval {
switch self {
case .timer: return 60 * 25
case .break: return 60 * 5
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.