Skip to content

Instantly share code, notes, and snippets.

View giovani-pereira-ifood's full-sized avatar

Giovani Nascimento Pereira giovani-pereira-ifood

View GitHub Profile
@giovani-pereira-ifood
giovani-pereira-ifood / instagramStories.txt
Last active February 7, 2020 19:31
instagramStories.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram-stories</string>
</array>
@giovani-pereira-ifood
giovani-pereira-ifood / user_known.swift
Last active July 27, 2020 17:00
User codable struct with favorite color field
struct User: Codable {
let name: String
let color: Color
enum Color: String, Codable {
case red = "RED"
case yelllow = "YELLOW"
case blue = "BLUE"
}
}
// This data contains the following JSON data
// {
// "name": "Mario",
// "color": "YELLOW"
// }
let data = requestData()
let decoder = JSONDecoder()
let object = try? decoder.decode(User.self, from: data)
// {
// "name": "Janine",
// "color": "GREEN"
// }
let data = requestData()
let decoder = JSONDecoder()
let object = try? decoder.decode(User.self, from: data)
print(object) // nil
// {
// "name": "Kermaine",
// "color": "RED",
// "number": 14
// }
let data = requestData()
let decoder = JSONDecoder()
let object = try? decoder.decode(User.self, from: data)
struct User: Codable {
let name: String
let color: Color?
enum Color: String, Codable {
case red = "RED"
case yelllow = "YELLOW"
case blue = "BLUE"
}
}
struct User: Codable {
let name: String
let preferences: [Preference]
}
struct Account: Codable {
let uuid: String
let email: String?
let address: Address?
}
protocol UnknownCase: RawRepresentable, CaseIterable where RawValue: Equatable & Encodable {
static var unknownCase: Self { get }
}
extension UnknownCase {
init(rawValue: RawValue) {
let value = Self.allCases.first { $0.rawValue == rawValue }
self = value ?? Self.unknownCase
}
struct User: Codable {
let name: String
let color: Color
enum Color: String, Codable, UnknownCase {
static let unknwonCase: Self = .unknown
case red = "RED"
case yelllow = "YELLOW"
case blue = "BLUE"
// {
// "name": "Janine",
// "color": "GREEN"
// }
let data = requestData()
let decoder = JSONDecoder()
let object = try? decoder.decode(User.self, from: data)
print(object) // User(name: "Janine", color: .unknown)