Skip to content

Instantly share code, notes, and snippets.

View randomguy0815's full-sized avatar

Alexander Anderl randomguy0815

View GitHub Profile
Decoding method Value exists Null Attribute missing
synthesized String property
decode(String, ...)
synthesized String? property
decode(String?, ...)
decodeIfPresent(String, ...)
struct BasicModelIfPresent: Decodable {
let someProperty: String?
   
enum Keys: CodingKey {
case someProperty
}
   
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
   
struct BasicModelOptionalCustom: Decodable {
let someProperty: String?
   
enum Keys: CodingKey {
case someProperty
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
   
struct BasicModelOptionalSynthesized: Decodable {
let someProperty: String?
}
{
"someProperty": null
}
{
"someProperty": "text"
}
struct BasicModelCustom: Decodable {
  let someProperty: String
   
  enum Keys: CodingKey {
  case someProperty
  }
   
  init(from decoder: Decoder) throws {
  let container = try decoder.container(keyedBy: Keys.self)
   
struct BasicModelSynthesized: Decodable {
  let someProperty: String
}
@randomguy0815
randomguy0815 / MantleToCodable.swift
Last active December 12, 2018 13:46
Mantle to JSON
typealias JSONDictionary = [String : Any]
typealias JSONArray = [Any]
struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}