Skip to content

Instantly share code, notes, and snippets.

@ole
ole / !swiftui-reflection-dump.md
Last active November 24, 2024 03:26
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.

<!--
XSLT for removing unused namespaces from an XML file.
Author: Dimitre Novatchev
Source: https://stackoverflow.com/a/4594626
License: CC BY-SA, https://creativecommons.org/licenses/by-sa/2.5/
Usage:
xmlstarlet tr remove-unused-namespaces.xslt -
@ole
ole / NotificationObserver.swift
Last active February 17, 2021 16:34
Demo that retaining self in a NotificationCenter observer block leads to a reference cycle you have to break manually.
// 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
ole / KeyedContainer.swift
Created December 10, 2019 09:55
A property wrapper for dictionaries with keys that are raw-representable as strings. It modifies the wrapped dictionary's encoding/decoding behavior such that the dictionary is encoded as a dictionary (unkeyed container) rather than as an array (keyed container). For context, see https://oleb.net/blog/2017/12/dictionary-codable-array/
import Foundation
// MARK: - KeyedContainer
/// A property wrapper for dictionaries with keys that are raw-representable as strings.
/// It modifies the wrapped dictionary's encoding/decoding behavior such that the dictionary
/// is encoded as a dictionary (unkeyed container) rather than as an array (keyed container).
///
/// For context, see <https://oleb.net/blog/2017/12/dictionary-codable-array/>.
@propertyWrapper
@ole
ole / combine-urlsession.swift
Last active November 7, 2019 02:53
Combine with URLSession.dataTaskPublisher
// https://twitter.com/BelleBCooper/status/1192173933983715328
import Combine
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
/// JSON format:
///
@ole
ole / x-by-y-operator.swift
Created October 29, 2019 09:30
Using × as a custom operator for creating "x-by-y" values such as CGPoint or CGSize.
precedencegroup XByYPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator ×: XByYPrecedence // U+00D7 MULTIPLICATION SIGN
// ------
import CoreGraphics
@ole
ole / let.swift
Created September 9, 2019 15:14
A replacement for let bindings in Swift function builders
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 / Atomic.swift
Created June 6, 2019 13:27
Atomic as a property wrapper
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 June 6, 2019 12:58
macOS Catalina mounts the read-only system volume at / and the read-write data volume at /System/Volumes/Data
% 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
@ole
ole / load-misaligned.swift
Last active June 25, 2019 21:54
Load misaligned data from a blob of bytes. I don't know if this is correct.
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 {