Skip to content

Instantly share code, notes, and snippets.

@itaiferber
Created January 31, 2018 15:39
Show Gist options
  • Save itaiferber/3a504c6abdc7d4e390815e0866a83782 to your computer and use it in GitHub Desktop.
Save itaiferber/3a504c6abdc7d4e390815e0866a83782 to your computer and use it in GitHub Desktop.
import Foundation
struct AnyEncodable: Encodable {
let value: Encodable
init(_ value: Encodable) {
self.value = value
}
func encode(to encoder: Encoder) throws {
try value.encode(to: encoder)
}
}
struct Model<T : Encodable> : Encodable {
let value: T
init(_ value: T) {
self.value = value
}
}
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(formatter)
let now = Date()
let d1 = try encoder.encode(Model(now))
let d2 = try encoder.encode(Model(AnyEncodable(now)))
print(String(data: d1, encoding: .utf8)!)
print(String(data: d2, encoding: .utf8)!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment