Skip to content

Instantly share code, notes, and snippets.

@kumamotone
Created March 21, 2018 17:56
Show Gist options
  • Save kumamotone/11390df03fb447766086a654114805c6 to your computer and use it in GitHub Desktop.
Save kumamotone/11390df03fb447766086a654114805c6 to your computer and use it in GitHub Desktop.
A Codable struct converting a Int value to Bool.
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