Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created April 27, 2018 02:24
Show Gist options
  • Save azamsharp/3c2c0a5bd6d1fe549eb2b5d3979a80d6 to your computer and use it in GitHub Desktop.
Save azamsharp/3c2c0a5bd6d1fe549eb2b5d3979a80d6 to your computer and use it in GitHub Desktop.
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