Created
April 27, 2018 02:24
-
-
Save azamsharp/3c2c0a5bd6d1fe549eb2b5d3979a80d6 to your computer and use it in GitHub Desktop.
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 AnyDecodable : Decodable { | |
let value :Any | |
public init<T>(_ value :T?) { | |
self.value = value ?? () | |
} | |
public init(from decoder :Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
if let string = try? container.decode(String.self) { | |
self.init(string) | |
} else if let int = try? container.decode(Int.self) { | |
self.init(int) | |
} else { | |
self.init(()) | |
} | |
// handle all the different types including bool, array, dictionary, double etc | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment