View medium-codable-response-wrapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data = """ | |
{ | |
"response": [ | |
{ | |
"name": "Kitty", | |
}, | |
{ | |
"name": "Doggy", | |
} | |
] |
View medium-codable-animal.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let exampleData = """ | |
[ | |
{ | |
"type": "cat", | |
"name": "Kitty", | |
}, | |
{ | |
"type": "dog", | |
"name": "Doggy", | |
} |
View medium-codable-lossyarray.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct LossyArray<Element: Decodable>: Decodable { | |
private(set) var elements: [Element] | |
init(from decoder: Decoder) throws { | |
var container = try decoder.unkeyedContainer() | |
var elements = [Element]() | |
if let count = container.count { | |
elements.reserveCapacity(count) | |
} |
View medium-codable-dynamic-type.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct AnimalContainer: Decodable { | |
let animal: Animal | |
enum AnimalType: String, Decodable { | |
case cat | |
case dog | |
} | |
enum CodingKeys: String, CodingKey { | |
case type |
View medium-codable-strategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UserSettings: Codable { | |
let someBool: Bool | |
let someDate: Date | |
enum CodingKeys: String, CodingKey { | |
case someBool = "someDifferentKeyBool" | |
case someDate = "someDifferentKeyDate" | |
} | |
} |
View medium-codable-2.2.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e |
View medium-codable-urlsession.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension URLSession { | |
func send<T: Decodable>(_ request: URLRequest, | |
completion: @escaping ((Result<T, Error>) -> Void)) { | |
let task = dataTask(with: request) { (data, response, error) in | |
if let data = data { | |
do { | |
let decoded = try JSONDecoder().decode(T.self, from: data) | |
completion(.success(decoded)) | |
} | |
catch let error { |
View medium-codable-userdefaults.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UserSettings: Codable { | |
let someBool: Bool | |
let someDate: Date | |
} | |
var userSettings: UserSettings? { | |
get { | |
guard let data = UserDefaults.standard.object(forKey: "userSettings") as? Data else { return nil } | |
return try? JSONDecoder().decode(UserSettings.self, from: data) | |
} |
View git-commit-auto-msg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Do git add and commit with auto generated message. For laziness reasons 😂. | |
# It simply reads `git status -s`, converts the beginning M -> Update, A -> Add, and D -> Delete, and use it as a commit message. | |
# Example: | |
# git status: M updated.txt | |
# generated message: Update updated.txt | |
# | |
# For debugging sed part: | |
# echo "M updated.txt\nA added.txt\nD deleted.txt\nR events/BaseEvents.yml -> events/AppEvents.yml" | sed "s/^M /Update/;s/^A /Add/;s/^D /Delete/;s/^R /Rename/;s/->/to/" | |
alias gaca='git add .; git status -s | sed "s/^M /Update/;s/^A /Add/;s/^D /Delete/;s/^R /Rename/;s/->/to/" | git commit --file -' |