Skip to content

Instantly share code, notes, and snippets.

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)
View pattern-matching.swift
/// Ignores `value` and just evaluates `pattern`.
/// Can be used to evaluate arbitrary expressions in a switch.
func ~=<A>(pattern: @autoclosure () -> Bool, value: A) -> Bool {
return pattern()
let (x, y, z) = (0, 0, 1)
switch () /* can be anything */ {
case x > 0: print("x matches")
View UIBezierPath-cutout.swift
import UIKit
let containingRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let path1 = UIBezierPath(rect: containingRect)
let circleRect = CGRect(x: 50, y: 10, width: 80, height: 80)
let circleCenter = CGPoint(x: circleRect.midX, y: circleRect.midY)
let radius = circleRect.height / 2
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 Apr 23, 2018
A script for setting `UIFileSharingEnabled` and `LSSupportsOpeningDocumentsInPlace` in Xcode debug builds.
if ( [ "$CONFIGURATION" == "Debug" ] ) then
echo "Enabling filesharing flags for '$CONFIGURATION' configuration in "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}""
/usr/libexec/PlistBuddy -c "Set :UIFileSharingEnabled true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :LSSupportsOpeningDocumentsInPlace true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
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
ole / units-and-locales.swift
Last active Mar 12, 2020
Paste into a playground.
View units-and-locales.swift
import Foundation
// Playing around with MeasurementFormatter.string(from: Unit)
let f = MeasurementFormatter()
f.locale = Locale(identifier: "de_DE")
f.unitOptions = .naturalScale
f.unitStyle = .long
f.string(from: UnitSpeed.milesPerHour)
f.string(from: UnitTemperature.celsius)
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.
ole / URLSession.swift
Created Feb 22, 2018
Paste this into a playground and let it run for 5 seconds. You’ll get a non-nil response *and* error.
View URLSession.swift
import Foundation
import PlaygroundSupport
let bigFile = URL(string: "")!
let task = URLSession.shared.dataTask(with: bigFile) { (data, response, error) in
print("data: \(data)")
print("response: \(response)")
print("error: \(error)")
View even-odd-indices.swift
let s = "Hello World"
let evenIndices = s.indices.enumerated()
.filter { $0.offset % 2 == 0 }
.map { $0.element }
for idx in evenIndices {