Skip to content

Instantly share code, notes, and snippets.

// I learned about `#if canImport(module, _version: x.y)` from Tony Allevato:
// https://forums.swift.org/t/pitch-sdk-conditional-code/52642/4
//
// Does this work reliably? I have no idea!
// I have only done some quick experiments in a playground.
//
// The Foundation version in Xcode 14.0b1 is:
// - in iOS's Foundation.swiftinterface: 1932.104
// - in macOS's Foundation.swiftinterface: 1932.401
//
@ole
ole / Text+Link.swift
Created May 12, 2022 10:50
Add links in SwiftUI Text views via a custom string interpolation.
@ole
ole / MyMainActor.swift
Last active October 23, 2023 20:46
A reimplementation of the basics of MainActor. Sample code for https://oleb.net/2022/how-mainactor-works/
import Dispatch
@globalActor
final actor MyMainActor {
// Don’t allow others to create instances
private init() {}
// Requirements from the implicit GlobalActor conformance
typealias ActorType = MyMainActor
@ole
ole / HeterogeneousDictionary.swift
Last active May 17, 2023 19:23
Code for my article "A heterogeneous dictionary with strong types in Swift" https://oleb.net/2022/heterogeneous-dictionary/
// A heterogeneous dictionary with strong types in Swift, https://oleb.net/2022/heterogeneous-dictionary/
// Ole Begemann, April 2022
/// A key in a `HeterogeneousDictionary`.
public protocol HeterogeneousDictionaryKey {
/// The "namespace" the key belongs to. Every `HeterogeneousDictionary` has its associated domain,
/// and only keys belonging to that domain can be stored in the dictionary.
associatedtype Domain
/// The type of the values that can be stored under this key in the dictionary.
associatedtype Value
@ole
ole / TaskSignaling.swift
Last active December 23, 2021 19:15
Using CheckedContinuation to communicate between concurrent tasks.
// This is the code for https://forums.swift.org/t/communicating-between-two-concurrent-tasks/54240
import _Concurrency
actor Buffer {
var elements: [Int] = []
private var isNotEmpty: CheckedContinuation<Void, Never>? = nil
deinit {
// TODO: If the continuation is not nil,
@ole
ole / Channel.swift
Last active December 23, 2021 13:27
Using CheckedContinuation to communicate between two concurrent tasks.
import _Concurrency
actor Channel<Output> {
private var conditionVar: CheckedContinuation<Output, Never>? = nil
deinit {
// TODO: if conditionVar != nil, resume it by throwing `CancellationError()`?
}
/// If there's no receiver, the sent value will be lost.
// A view that can flip between its "front" and "back" side.
//
// Animation implementation based on:
// Chris Eidhof, Keyframe animations <https://gist.github.com/chriseidhof/ea0e435197f550b195bb749f4777bbf7>
import SwiftUI
// MARK: - Chris's keyframe animation design
struct Keyframe<Data: Animatable> {
@ole
ole / TwoColumnNavigation.swift
Created December 2, 2021 17:48
Weird SwiftUI two-column NavigationView behavior
// 1. Run on iPad simulator in landscape (= two-column navigation view).
// 2. Drill down to level 3
// 3. Observe console output.
//
// Result with .navigationViewStyle(.stack) (as expected):
// Level 1: drilledDown=false
// Level 1: drilledDown=true
// Level 2: drilledDown=false
// Level 2: drilledDown=true
// Level 3: drilledDown=false
import SwiftUI
struct ContentView: View {
@State var horizontal: Bool = true
@Namespace var namespace
var body: some View {
VStack(spacing: 40) {
if horizontal {
HStack { items }
@ole
ole / CodableContainers.swift
Created November 18, 2021 10:55
Codable: Encoding through a single-value container vs. calling `encode(to: encoder)` directly
import Foundation
struct ContainerEncoded: Encodable {
var date: Date = .now
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(date)
}
}