Skip to content

Instantly share code, notes, and snippets.

@ole
ole / thirty-days-of-metal.md
Last active February 17, 2026 08:29
Warren Moore – Thirty Days of Metal
@ole
ole / swift-has-feature.sh
Last active January 25, 2026 01:23
swift-list-features: List Swift compiler upcoming and experimental feature flags. If you're using Swift 6.2 or later, `swift -print-supported-features` does something very similar, but only for the compiler version you have installed. · swift-has-feature: Check if a given compiler knows a specific feature flag, and whether it's an upcoming or ex…
#!/bin/zsh
# Test if the Swift compiler knows about a particular language feature.
#
# Usage:
#
# swift-has-feature [--swift SWIFT_PATH] [--language-version LANGUAGE_VERSION] FEATURE
#
# The feature should be an upcoming or experimental language feature,
# such as `"StrictConcurrency"` or `"ExistentialAny"`.
@ole
ole / Package.swift
Last active December 3, 2025 09:41
SwiftPM Package.swift with upcoming features enabled
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"]),
],
@ole
ole / !swiftui-reflection-dump.md
Last active November 30, 2025 22:27
A dump of the SwiftUI.framework binary for the iOS simulator (as of Xcode 12.0 beta 2) using the swift-reflection-dump tool.

A dump of the SwiftUI.framework binary for the iOS simulator (as of Xcode 12.0 beta 2) using the swift-reflection-dump tool.

Note: I used a Swift 5.3 compiler build from a few weeks ago that I had laying around. Because of ABI stability, I don't think the swift-reflection-dump version has to match the compiler version that was used to build the binary, but I'm not 100% sure.

@ole
ole / update_storyboard_strings.sh
Last active November 14, 2025 17:33
Automatically extract translatable strings from Xcode storyboards and update .strings files. Original version by MacRumors forum user mikezang (http://forums.macrumors.com/showpost.php?p=16060008&postcount=4). Slightly updated by Ole Begemann. NOTE: this Gist moved to a regular repo at https://github.com/ole/Storyboard-Strings-Extraction.
# (File moved to https://github.com/ole/Storyboard-Strings-Extraction)
@ole
ole / UserDefaultsAsyncSequence.swift
Last active October 31, 2025 09:06
UserDefaults KVO observation with AsyncSequence/AsyncStream
// UserDefaults KVO observation with AsyncSequence/AsyncStream
// Ole Begemann, 2023-04
// Updated for Swift 6, 2024-11
// https://gist.github.com/ole/fc5c1f4c763d28d9ba70940512e81916
import Foundation
// This is ugly, but UserDefaults is documented to be thread-safe, so this
// should be OK.
extension UserDefaults: @retroactive @unchecked Sendable {}
@ole
ole / XCTExpectation.swift
Last active October 30, 2025 15:54
A variant of XCTKVOExpectation that works with native Swift key paths. To try it out, paste the code into an Xcode playground and observe the unit test output in the console. See my blog post at https://oleb.net/blog/2018/02/xctkvoexpectation-swift-keypaths/
import XCTest
/// An expectation that is fulfilled when a Key Value Observing (KVO) condition
/// is met. It's variant of `XCTKVOExpectation` with support for native Swift
/// key paths.
final class KVOExpectation: XCTestExpectation {
private var kvoToken: NSKeyValueObservation?
/// Creates an expectation that is fulfilled when a KVO change causes the
/// specified key path of the observed object to have an expected value.
@ole
ole / HeterogeneousDictionary.swift
Last active October 7, 2025 02:19
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
/// Runs a secondary side action concurrently with a primary action.
/// The secondary action only starts after the specified delay.
///
/// Usage example: showing a progress indicator UI only if the primary action
/// takes a significant amount of time.
///
/// The secondary action runs in a child task (structured concurrency).
///
/// - Note: I'd like to provide a default argument for the clock: `clock: C = .continuous`,
/// like `Task.sleep` does. But this doesn't compile in Swift 6.2. Related bug report:
@ole
ole / swift-list-diagnostic-groups.sh
Last active August 25, 2025 12:31
swift-list-diagnostic-groups.sh: Shell script for listing the names of diagnostic groups in the Swift compiler.
#!/bin/bash
# List Swift diagnostic groups.
#
# Author: Ole Begemann
#
# Usage:
#
# swift-list-diagnostic-groups [version] # default: main branch
#