Skip to content

Instantly share code, notes, and snippets.

Ole Begemann ole

Block or report user

Report or block ole

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ole
ole / UIAlertController+TextField.swift
Last active Nov 15, 2019
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: https://oleb.net/2018/uialertcontroller-textfield/
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
ole / combine-urlsession.swift
Last active Nov 7, 2019
Combine with URLSession.dataTaskPublisher
View combine-urlsession.swift
// https://twitter.com/BelleBCooper/status/1192173933983715328
import Combine
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
/// JSON format:
///
@ole
ole / let.swift
Created Sep 9, 2019
A replacement for let bindings in Swift function builders
View let.swift
import SwiftUI
func `let`<Value, Return>(_ expression: Value, body: (Value) -> Return) -> Return {
body(expression)
}
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
`let`(geometry.size.width / 2) { lineWidth in
@ole
ole / x-by-y-operator.swift
Created Oct 29, 2019
Using × as a custom operator for creating "x-by-y" values such as CGPoint or CGSize.
View x-by-y-operator.swift
precedencegroup XByYPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator ×: XByYPrecedence // U+00D7 MULTIPLICATION SIGN
// ------
import CoreGraphics
@ole
ole / AsyncOperation.swift
Created Aug 19, 2018
An (NS)Operation subclass for async operations
View AsyncOperation.swift
import Foundation
/// An abstract class that makes building simple asynchronous operations easy.
/// Subclasses must override `main()` to perform any work and call `finish()`
/// when they are done. All `NSOperation` work will be handled automatically.
///
/// Source/Inspiration: https://stackoverflow.com/a/48104095/116862 and https://gist.github.com/calebd/93fa347397cec5f88233
open class AsyncOperation: Operation {
public init(name: String? = nil) {
super.init()
@ole
ole / XCTExpectation.swift
Last active Aug 8, 2019
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/
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
ole / load-misaligned.swift
Last active Jun 25, 2019
Load misaligned data from a blob of bytes. I don't know if this is correct.
View load-misaligned.swift
import Foundation
let data = Data([0, 0, 0, 0, 0, 0x11, 0x22, 0x33, 0x44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
data.count
extension UnsafeRawBufferPointer {
func loadUnaligned<T>(fromByteOffset offset: Int, as: T.Type) -> T {
// Allocate correctly aligned memory and copy bytes there
let alignedPointer = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<T>.stride, alignment: MemoryLayout<T>.alignment)
defer {
@ole
ole / Mojave-dynamic-wallpaper-notes.md
Last active Jun 8, 2019
Reverse-engineering the dynamic wallpaper file format in macOS Mojave.
View Mojave-dynamic-wallpaper-notes.md

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 / Atomic.swift
Created Jun 6, 2019
Atomic as a property wrapper
View Atomic.swift
import Dispatch
import PlaygroundSupport
@propertyDelegate
struct Atomic<A> {
private var _value: A
private let queue = DispatchQueue(label: "property wrapper")
init(initialValue: A) {
_value = initialValue
@ole
ole / mount.txt
Created Jun 6, 2019
macOS Catalina mounts the read-only system volume at / and the read-write data volume at /System/Volumes/Data
View mount.txt
% mount
/dev/disk1s6 on / (apfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
/dev/disk1s5 on /System/Volumes/Data (apfs, local, journaled, nobrowse)
/dev/disk1s4 on /private/var/vm (apfs, local, journaled, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
% ls /System/Volumes/Data
Device Users
You can’t perform that action at this time.