Skip to content

Instantly share code, notes, and snippets.

@seanwoodward
seanwoodward / alignment-guides-tool.swift
Last active March 10, 2025 05:01 — forked from swiftui-lab/alignment-guides-tool.swift
A few updates to such a great example
// The SwiftUI Lab
// Website: https://swiftui-lab.com
// Article: https://swiftui-lab.com/alignment-guides
import SwiftUI
import Observation
extension EnvironmentValues {
@Entry var alignmentModel: Model = Model()
}
@seanwoodward
seanwoodward / Tuple.swift
Last active March 19, 2024 04:36
Tuple struct using Parameter Packs to facilitate things like using a tuple as keys in dictionaries or values in sets and heaps.
#if swift(>=5.9)
import Foundation
public struct Tuple<T, each U> {
private let head: T
private let tail: (repeat (each U))
public var values: (T, repeat each U) {
@seanwoodward
seanwoodward / uuid.sh
Created November 3, 2023 10:15 — forked from ultim8k/uuid.sh
Generate UUID in BASH/ZSH
### Source: https://coderwall.com/p/t_sz3q/generate-uuid-at-shell-prompt
# alias uuid="python -c 'import sys,uuid; sys.stdout.write(str(uuid.uuid4()))' | pbcopy && pbpaste && echo"
alias uuid="uuidgen | tr -d '\n' | tr '[:upper:]' '[:lower:]' | pbcopy && pbpaste && echo"
@seanwoodward
seanwoodward / OrdinalNumberFormatStyle.swift
Last active November 4, 2023 22:53
Quick and dirty FormatStyle for ordinal representation of a given Int
struct OrdinalNumberFormatStyle: FormatStyle {
func format(_ value: Int) -> String {
Self.ordinalFormatter.string(from: NSNumber(value: value)) ?? String(describing: value)
}
static var ordinalFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .ordinal
formatter.locale = .autoupdatingCurrent
return formatter
@seanwoodward
seanwoodward / AppStorage.Key++.swift
Created March 28, 2022 15:41
Extend AppStorage property wrapper to use typed keys instead of strings
import SwiftUI
extension AppStorage {
public struct Key {
let name: String
}
}
extension AppStorage {
public init(wrappedValue: Value, _ key: AppStorage.Key, store: UserDefaults? = nil) where Value == Bool {
@seanwoodward
seanwoodward / ContactPickerButton.swift
Last active May 27, 2024 03:31
Using CNContactPickerViewController from SwiftUI View
import SwiftUI
import Contacts
import ContactsUI
struct SomeView: View {
@State var contact: CNContact?
var body: some View {
VStack {
Text("Selected: \(contact?.givenName ?? "")")
@seanwoodward
seanwoodward / UpdateKeyBindingSet.sh
Last active March 28, 2022 13:48
Shell Script for Updating Xcode IDETextKeyBindingSet.plist with custom actions: Insert New Line Above, Insert New Line Below, and Duplicate Current Line
# update with the path to your file,
# /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist
KEYBINDINGSET='IDETextKeyBindingSet.plist'
plutil -insert 'User Defined' -dictionary -- "$KEYBINDINGSET"
# ^⇧⏎
plutil -insert 'User Defined'.'Insert New Line Above Current Line' -string 'moveUp:, moveToEndOfParagraph:, insertParagraphSeparator:' -- "$KEYBINDINGSET"
# ^⌥⏎
plutil -insert 'User Defined'.'Insert New Line Below Currnt Line' -string 'moveToEndOfParagraph:, insertParagraphSeparator:' -- "$KEYBINDINGSET"
# ⌘D
@seanwoodward
seanwoodward / custom-shortcuts.txt
Last active October 15, 2021 16:40 — forked from CodeSlicing/custom-shortcuts.txt
Dictionary entries for CodeSlicing episode on custom shortcuts
<key>User Defined</key>
<dict>
<key>Insert New Line Above Current Line</key>
<string>moveUp:, moveToEndOfParagraph:, insertParagraphSeparator:</string>
<key>Insert New Line Below Currnt Line</key>
<string>moveToEndOfParagraph:, insertParagraphSeparator:</string>
<key>Duplicate Current Line Down</key>
<string>selectParagraph:, delete:, yank:, moveToBeginningOfParagraph:, yank:, moveUp:, moveToEndOfParagraph:</string>
</dict>
@seanwoodward
seanwoodward / BindingSample.swift
Created September 29, 2021 17:45
SwiftUI @binding and @State properties with custom initialization
import SwiftUI
struct CustomDatePickerExample: View {
@Binding private var currentDate: Date
@State private var currentYear: Int
@State private var currentMonth: Int
var body: some View {
@seanwoodward
seanwoodward / Collection+random(pick:).swift
Last active June 7, 2019 03:19
Pick a number of random elements from an array. Better way?
extension Collection {
func random(pick: Int) -> [Element] {
return self.indices.shuffled().prefix(pick).map { self[$0] }
}
}