Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
protocol DictionaryConvertible {
var dictionaryRepresentation: [String: Any] { get }
protocol DictionaryDecodable {
init?(dictionary: [String: Any])
typealias DictionaryRepresentable = DictionaryConvertible & DictionaryDecodable
extension DictionaryConvertible where Self: Encodable {
var dictionaryRepresentation: [String: Any] {
let data = try! JSONEncoder().encode(self)
return try! JSONSerialization.jsonObject(with: data, options: []) as! [String : Any]
extension DictionaryDecodable where Self: Decodable {
init?(dictionary: [String: Any]) {
guard let data = try? dictionary, options: []) else { return nil }
guard let info = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
self = info
struct AutocompleteResult: Codable, DictionaryRepresentable {
let text: String
let suggestions: [String]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.