Skip to content

Instantly share code, notes, and snippets.

View DhavalDobariya86's full-sized avatar

Dhaval Dobariya DhavalDobariya86

View GitHub Profile
@DhavalDobariya86
DhavalDobariya86 / Codable.swift
Last active April 11, 2020 20:12
Simple use of Codable to encode/decode JSON
import UIKit
struct Country: Codable {
let name: String
let capital: String
let callingCodes: [String]
let population: Double
let area: Double
let borders: [String]
let region: String
@DhavalDobariya86
DhavalDobariya86 / Codable+RenamedParameters.swift
Last active April 11, 2020 20:16
Codable with renamed parameters
import UIKit
struct Country: Codable {
let name: String
let capital: String
let alternateSpellings: [String]
enum CodingKeys: String, CodingKey {
case name
case capital
@DhavalDobariya86
DhavalDobariya86 / Codable+KeyDecodingStrategy.swift
Created April 11, 2020 20:18
Codable with Key decoding strategy
import UIKit
struct Country: Codable {
let name: String
let capital: String
let callingCodes: [String]
}
let jsonData: Data = """
{
@DhavalDobariya86
DhavalDobariya86 / Codable+DateDecodingStrategy.swift
Created April 11, 2020 20:20
Codable with Date decoding strategy
import UIKit
struct Country: Codable {
let name: String
let capital: String
let independanceDay: Date
}
let jsonData: Data = """
{
@DhavalDobariya86
DhavalDobariya86 / Codable+ExcludeKeys.swift
Created April 11, 2020 20:23
Codable to exclude keys by removing it from parameter list
import UIKit
struct Country: Codable {
let name: String
let capital: String
}
let jsonData: Data = """
{
"name": "United States of America",
@DhavalDobariya86
DhavalDobariya86 / Codable+ExcludeKeys+CodingKeys.swift
Created April 11, 2020 20:25
Codable to exclude keys by removing it from CodingKeys enum
import UIKit
struct Country: Codable {
let name: String
let capital: String
let alternateSpellings: [String]
enum CodingKeys: String, CodingKey {
case name
case capital
@DhavalDobariya86
DhavalDobariya86 / Codable+OptionalFields.swift
Created April 11, 2020 20:27
Codable to handle optional properties from JSON
import UIKit
struct Employee: Codable {
let name: String
let phoneNumber: String?
}
let jsonDataWithPhoneNumber: Data = """
{
"name": "Eric",
@DhavalDobariya86
DhavalDobariya86 / Codable+NestedJSON.swift
Last active April 11, 2020 20:35
Codable to decode required properties only from deeply nested JSON
import UIKit
struct Employee: Decodable {
let name: String
let city: String
enum CodingKeys: String, CodingKey {
case name
case city
case contactInformation
@DhavalDobariya86
DhavalDobariya86 / Country.swift
Last active June 7, 2020 18:53
MVVM-C Model
import Foundation
struct Country: Codable, Equatable, CountryListItemRepresentable, CountryDetailRepresentable {
let name: String
let capital: String
let region: String
let subregion: String
let timezones: [String]
let borders: [String]
import Foundation
private struct CountryListEndpoint: EndpointReprsentable {
var httpMethod: HTTPMethod { .get }
var urlPath: URLPath { .name }
let pathComponent: String?
}