Created
March 21, 2018 17:56
-
-
Save kumamotone/11390df03fb447766086a654114805c6 to your computer and use it in GitHub Desktop.
A Codable struct converting a Int value to Bool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct IntToBool: Codable { | |
public var value: Bool | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
let intValue = try container.decode(Int.self) | |
self.value = (intValue != 0) | |
} | |
public init(value: Bool) { | |
self.value = value | |
} | |
public func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
try container.encode(value.description) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment