You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've added a Bool type auto-conversion function, so if people want to add it to their projects, I'll try pulling requests.
JSON
Swift
"false", "no", "0", "n", "f", 0, false
false
"true", "yes", "1", "y", "t", 1, true
true
import Foundation
@propertyWrapperpublicstructBoolValue:Codable{publicvarwrappedValue:Boolpublicinit(from decoder:Decoder)throws{letcontainer=try decoder.singleValueContainer()
if let stringifiedValue =try? container.decode(String.self){
switch stringifiedValue.lowercased(){case"false","no","0","n","f": wrappedValue = false
case"true","yes","1","y","t": wrappedValue = true
default:letdescription="Expected to decode Bool but found a '\(stringifiedValue)' instead."throwDecodingError.dataCorruptedError(in: container, debugDescription: description)}}else if let integerifiedValue =try? container.decode(Int.self){
switch integerifiedValue {case0: wrappedValue = false
case1: wrappedValue = true
default:letdescription="Expected to decode Bool but found a '\(integerifiedValue)' instead."throwDecodingError.dataCorruptedError(in: container, debugDescription: description)}}else{
wrappedValue =try container.decode(Bool.self)}}publicfunc encode(to encoder:Encoder)throws{try wrappedValue.encode(to: encoder)}}
Usage:
@BoolValuevarisFollow:Bool
The text was updated successfully, but these errors were encountered:
I've added a
Bool
type auto-conversion function, so if people want to add it to their projects, I'll try pulling requests.Usage:
The text was updated successfully, but these errors were encountered: