Skip to content

Instantly share code, notes, and snippets.

ole / core-data-backup.swift
Last active Jul 22, 2021
How to make a copy of a Core Data SQLite database. See for more.
View core-data-backup.swift
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
View LazyFilterSequenceIndices.swift
let evens = Array(1...10)
.filter { $0.isMultiple(of: 2) }
evens.indices // → [1, 3, 5, 7, 9]
evens[0] // → 1 (🤯!)
ole / !
Last active Jul 6, 2021
A dump of the SwiftUI.framework binary for the iOS simulator (as of Xcode 12.0 beta 2) using the swift-reflection-dump tool.
View !

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 / DateFormatting.swift
Created Jun 18, 2021
ISO8601 date formatting in Foundation in iOS 15/macOS 12
View DateFormatting.swift
import Foundation
let date =
date.formatted(.iso8601) // "20210618T191800Z"
date.formatted(.iso8601.year().month().day().dateSeparator(.dash)) // "2021-06-18"
date.formatted(.iso8601.dateSeparator(.dash).timeSeparator(.colon)) // "2021-06-18T19:18:00Z"
ole /
Last active May 14, 2021
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 /
Last active May 9, 2021
Automatically extract translatable strings from Xcode storyboards and update .strings files. Original version by MacRumors forum user mikezang ( Slightly updated by Ole Begemann. NOTE: this Gist moved to a regular repo at
# (File moved to
ole / UIAlertController+TextField.swift
Last active Apr 24, 2021
A UIAlertController with a text field and the ability to perform validation on the text the user has entered while the alert is on screen. The OK button is only enabled when the entered text passes validation. More info:
View UIAlertController+TextField.swift
import UIKit
/// A validation rule for text input.
public enum TextValidationRule {
/// Any input is valid, including an empty string.
case noRestriction
/// The input must not be empty.
case nonEmpty
/// The enitre input must match a regular expression. A matching substring is not enough.
case regularExpression(NSRegularExpression)
ole / AlignmentGuideLayout.swift
Created Mar 28, 2021
SwiftUI alignment guide layout. Why is the rectangle drawn out of bounds of the stack view?
View AlignmentGuideLayout.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
// Why is the rectangle drawn out of bounds of the stack view?
.frame(width: 100, height: 40)
.alignmentGuide( { _ in 0 }
ole / NotificationObserver.swift
Last active Feb 17, 2021
Demo that retaining self in a NotificationCenter observer block leads to a reference cycle you have to break manually.
View NotificationObserver.swift
// Paste into a macOS playground in Xcode and run.
// Deleting line 46 "c?.stopObserving()" triggers the assertion because
// the reference cycle caused by retaining self in the observer block is never broken.
import Foundation
let myNotification = NSNotification.Name(rawValue: "myNotification")
var cHasBeenDeallocated = false
ole / XCTExpectation.swift
Last active Jan 21, 2021
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
View XCTExpectation.swift
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.