Skip to content

Instantly share code, notes, and snippets.

@ole
ole / GroupWithTask.swift
Last active November 9, 2023 13:47
SwiftUI: Group { … }.task { … }
View GroupWithTask.swift
import SwiftUI
@main
struct GroupWithTaskApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
@ole
ole / ExtensibleFloatingPointFormatStyle.swift
Created September 9, 2023 13:33
An "extension" of FloatingPointFormatStyle that adds a `minusSign` API to customize the character(s) used as the minus sign. You could add additional extensions by following the same pattern. This demonstrates that "extending" the built-in format styles requires a lot of boilerplate because you have to replicate their APIs in your own type.
View ExtensibleFloatingPointFormatStyle.swift
import Foundation
/// An "extension" of FloatingPointFormatStyle that adds a `minusSign` API to customize
/// the character(s) used as the minus sign.
///
/// You could add additional extensions by following the same pattern.
///
/// All other APIs are copied from FloatingPointFormatStyle and forward to it for their
/// implementation. This isn’t a full replica of the FloatingPointFormatStyle API, though,
/// because it’s only intended as a proof of concept.
@ole
ole / GenericOrdinalFormatStyle.swift
Created July 19, 2023 08:36
Generic ordinal format style for all BinaryInteger types
View GenericOrdinalFormatStyle.swift
import Foundation
extension FormatStyle {
static func ordinal<FormatInput: BinaryInteger>() -> OrdinalFormatStyle<FormatInput>
where Self == OrdinalFormatStyle<FormatInput>
{
OrdinalFormatStyle()
}
}
@ole
ole / thirty-days-of-metal.md
Last active November 30, 2023 15:20
Warren Moore – Thirty Days of Metal
View thirty-days-of-metal.md
@ole
ole / NSHostingViewGenerics.swift
Created May 5, 2023 18:21
Avoiding AnyView when using NSHostingView with arbitrary content. Finding a way to specify the generic parameter.
View NSHostingViewGenerics.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
FrameworkView1 {
Text("FrameworkView1")
}
.border(.red)
@ole
ole / UserDefaultsAsyncSequence.swift
Last active November 2, 2023 12:20
UserDefaults KVO observation with AsyncSequence/AsyncStream
View UserDefaultsAsyncSequence.swift
// UserDefaults KVO observation with AsyncSequence/AsyncStream
// Ole Begemann, 2023-04
// https://gist.github.com/ole/fc5c1f4c763d28d9ba70940512e81916
import Foundation
extension UserDefaults {
func observeKey<Value>(_ key: String, valueType _: Value.Type) -> AsyncStream<Value?> {
var continuation: AsyncStream<Value?>.Continuation? = nil
let stream = AsyncStream(Value?.self) {
@ole
ole / RelativeSizeLayout.swift
Last active October 31, 2023 07:48
A SwiftUI layout and modifier for working with relative sizes ("50 % of your container"). https://oleb.net/2023/swiftui-relative-size/
View RelativeSizeLayout.swift
import SwiftUI
extension View {
/// Proposes a percentage of its received proposed size to `self`.
///
/// This modifier multiplies the proposed size it receives from its parent
/// with the given factors for width and height.
///
/// If the parent proposes `nil` or `.infinity` to us in any dimension,
/// we’ll forward these values to our child view unchanged.
@ole
ole / TaskGroupFireAndForget.swift
Last active March 22, 2023 16:27
Swift TaskGroup swallows errors if you use it for fire-and-forget tasks (i.e. you never await the child tasks)
View TaskGroupFireAndForget.swift
struct MyError: Error {}
func fireAndForget() async {
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
print("child task start")
print("child task throws")
throw MyError()
}
// Notice that we're not awaiting the child task.
@ole
ole / set-photos-keyboard-shortcuts.sh
Last active August 10, 2023 16:08
Assign a keyboard shortcut to the Export Unmodified Originals menu command in Photos.app on macOS
View set-photos-keyboard-shortcuts.sh
#!/bin/bash
# Assigns a keyboard shortcut to the Export Unmodified Originals
# menu command in Photos.app on macOS.
# @ = Command
# ^ = Control
# ~ = Option
# $ = Shift
shortcut='@~^e'
@ole
ole / FinderFileColors.swift
Created February 2, 2023 11:43
Assign colors (as shown in the macOS Finder) to files
View FinderFileColors.swift
import Foundation
enum FinderColor: Int {
case noColor = 0
case grey = 1
case green = 2
case purple = 3
case blue = 4
case yellow = 5
case red = 6