Code Snippets Swift

Short texts Swift 🧑‍💻 Code snippets Swift ✍️ Reading lists Swift 👀


struct SelectColor: View { let colours = [, .red, .orange, .yellow, .green, .mint, .teal, .cyan, .blue, .indigo, .purple, .pink, .gray, .brown, .white] @State var dix = 0 @Binding var selectedIndx: Int @Binding var selected:Bool @State private var solid = true @State private var wired = true @ObservedObject var cords = Cords.shared var body: some View […]

Asking Provisional Permissions for Push Notification

let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound, .badge, .provisional]) { granted, error in if let error = error { // Handle the error here. } // Provisional authorization granted. }

An auto-weak delegate for handle modern delegate pattern.

import Foundation /// A class that keeps a weakly reference for `self` when implementing `onXXX` behaviors. /// Instead of remembering to keep `self` as weak in a stored closure: /// /// “`swift /// // MyClass.swift /// var onDone: (() -> Void)? /// func done() { /// onDone?() /// } /// /// // ViewController.swift /// var […]


/* Why? – Lots of RawRepresentable boilerplate for every enum that needs a .other() case – The model is polluted with an invalid case which we can’t define behaviour for other than logging – New method lets us handle the invalid case (e.g. log it) but then only pass on valid cases to the rest […]

A rectangle shape for SwiftUI that can render any edge like a torn piece of paper.

// Copyright 2021 Kyle Hughes // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the “Software”), to deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of […]