Skip to content

Instantly share code, notes, and snippets.

View SwiftUI-ButtonModal
@State private var showModal = false
var body: some View {
Button(action: { self.showModal.toggle() }) {
HStack {
Text("Show MODAL")
Spacer()
Text("〉")
}
}.sheet(isPresented: $showModal) {
View medium-codable-response-wrapper.swift
let data = """
{
"response": [
{
"name": "Kitty",
},
{
"name": "Doggy",
}
]
View medium-codable-animal.swift
let exampleData = """
[
{
"type": "cat",
"name": "Kitty",
},
{
"type": "dog",
"name": "Doggy",
}
View medium-codable-lossyarray.swift
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
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
struct UserSettings: Codable {
let someBool: Bool
let someDate: Date
enum CodingKeys: String, CodingKey {
case someBool = "someDifferentKeyBool"
case someDate = "someDifferentKeyDate"
}
}
View medium-codable-urlsession.swift
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
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)
}
@hlung
hlung / git-commit-auto-msg
Last active Nov 6, 2019
Do git add and commit with auto generated message. For laziness reasons 😂.
View git-commit-auto-msg
# 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 -'