Skip to content

Instantly share code, notes, and snippets.

@ole
ole / GroupWithTask.swift
Last active November 9, 2023 13:47
SwiftUI: Group { … }.task { … }
import SwiftUI
@main
struct GroupWithTaskApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
@ole
ole / Text+Link.swift
Created May 12, 2022 10:50
Add links in SwiftUI Text views via a custom string interpolation.
@ole
ole / MyMainActor.swift
Last active October 23, 2023 20:46
A reimplementation of the basics of MainActor. Sample code for https://oleb.net/2022/how-mainactor-works/
import Dispatch
@globalActor
final actor MyMainActor {
// Don’t allow others to create instances
private init() {}
// Requirements from the implicit GlobalActor conformance
typealias ActorType = MyMainActor
@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.
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.
<!--
XSLT for removing unused namespaces from an XML file.
Author: Dimitre Novatchev
Source: https://stackoverflow.com/a/4594626
License: CC BY-SA, https://creativecommons.org/licenses/by-sa/2.5/
Usage:
xmlstarlet tr remove-unused-namespaces.xslt -
@ole
ole / GenericOrdinalFormatStyle.swift
Created July 19, 2023 08:36
Generic ordinal format style for all BinaryInteger types
import Foundation
extension FormatStyle {
static func ordinal<FormatInput: BinaryInteger>() -> OrdinalFormatStyle<FormatInput>
where Self == OrdinalFormatStyle<FormatInput>
{
OrdinalFormatStyle()
}
}
@ole
ole / CharacterArray.swift
Last active June 11, 2023 10:13
Two options for converting character ranges into arrays
// We can't use `Character` or `String` ranges directly because they aren't countable
// Create a countable range of ASCII values instead
let range = UInt8(ascii: "a")...UInt8(ascii: "z") // CountableClosedRange<UInt8>
// Convert ASCII codes into Character values
range.map { Character(UnicodeScalar($0)) } // Array<Character>
// → ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
@ole
ole / HeterogeneousDictionary.swift
Last active May 17, 2023 19:23
Code for my article "A heterogeneous dictionary with strong types in Swift" https://oleb.net/2022/heterogeneous-dictionary/
// A heterogeneous dictionary with strong types in Swift, https://oleb.net/2022/heterogeneous-dictionary/
// Ole Begemann, April 2022
/// A key in a `HeterogeneousDictionary`.
public protocol HeterogeneousDictionaryKey {
/// The "namespace" the key belongs to. Every `HeterogeneousDictionary` has its associated domain,
/// and only keys belonging to that domain can be stored in the dictionary.
associatedtype Domain
/// The type of the values that can be stored under this key in the dictionary.
associatedtype Value
@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.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
FrameworkView1 {
Text("FrameworkView1")
}
.border(.red)
@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)
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.