Skip to content

Instantly share code, notes, and snippets.

@ole
ole / swift-has-feature.sh
Last active May 9, 2024 13:32
List Swift compiler upcoming and experimental feature flags. (Note: the second script below, swift-list-features.sh, is probably the more useful one of the two. Unfortunately, I can't reorder them.)
#!/bin/zsh
# Test if the Swift compiler knows about a particular language feature.
#
# Usage:
#
# swift-has-feature [--swift SWIFT_PATH] FEATURE
#
# The exit code signals success (= the compiler knows this feature) or failure.
#
@ole
ole / set-photos-keyboard-shortcuts.sh
Last active May 4, 2024 02:10
Assign a keyboard shortcut to the Export Unmodified Originals menu command in Photos.app on macOS
#!/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 / thirty-days-of-metal.md
Last active May 1, 2024 19:19
Warren Moore – Thirty Days of Metal
@ole
ole / README.md
Created August 7, 2020 18:51
Extract a value from the SwiftUI environment by the name of its associated EnvironmentKey (e.g. "ForegroundColorKey").

Extract a value from the SwiftUI environment by the name of its associated EnvironmentKey (e.g. "ForegroundColorKey").

Tested only in Xcode 12.0 beta 4 in the iOS simulator. May break in other environments because it uses reflection. If SwiftUI's private type hierarchy changes, it will probably stop working.

@ole
ole / substr-mutation.swift
Created April 4, 2024 18:40
Mutating a Substring apparently makes a copy of the entire original String (in some scenarios)
// Make sure both the string and the substring are larger than 15 UTF-8 bytes
// to avoid the small string optimization
var str = "Hello world 1 Hello world 2 Hello world 3 Hello world 4 Hello world 5 Hello world 6 Hello world 7 Hello world 8 Hello world 9"
let prefixToStrip = 14
var substr = str.dropFirst(prefixToStrip).prefix(27)
let strToAppend = "+++APPENDED+++"
// ⚠️ It makes a difference how you mutate the Substring:
// - substr.append → Apparently makes a copy of the entire original string
// *and* even shifts the original string contents back to make room,
@ole
ole / RelativeSizeLayout.swift
Last active March 24, 2024 22:24
A SwiftUI layout and modifier for working with relative sizes ("50 % of your container"). https://oleb.net/2023/swiftui-relative-size/
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 / Mojave-dynamic-wallpaper-notes.md
Last active March 8, 2024 02:17
Reverse-engineering the dynamic wallpaper file format in macOS Mojave.

The dynamic wallpaper in MacOS Mojave is a single 114 MB .heic file that seems to contain 16 embedded images.

It also contains the following binary plist data in its metadata under the key "Solar". It's an array of 16 items, each with four keys:

  • i (integer). This seems to be the image index.
  • o (integer). This is always 1 or 0. Stephen Radford thinks it indicates dark mode (0) vs. light mode (1).
  • a (decimal). I’m pretty sure this is the angle of the sun over the horizon. 0º = sunset/sunrise. 90º = sun directly overhead. Negative values = sun below horizon.
  • z (decimal). This seems to be the cardinal position of the sun relative to the camera. 0º = sun is directly in front of the camera. 90º = sun is directly to the right of the camera. 180º = sun is directly behind the camera.
@ole
ole / UserDefaultsAsyncSequence.swift
Last active January 23, 2024 18:03
UserDefaults KVO observation with AsyncSequence/AsyncStream
// 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 / !swiftui-reflection-dump.md
Last active January 20, 2024 15:36
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 / core-data-backup.swift
Last active January 1, 2024 16:52
How to make a copy of a Core Data SQLite database. See https://oleb.net/blog/2018/03/core-data-sqlite-backup/ for more.
import CoreData
import Foundation
/// Safely copies the specified `NSPersistentStore` to a temporary file.
/// Useful for backups.
///
/// - Parameter index: The index of the persistent store in the coordinator's
/// `persistentStores` array. Passing an index that doesn't exist will trap.
///
/// - Returns: The URL of the backup file, wrapped in a TemporaryFile instance