Skip to content

Instantly share code, notes, and snippets.

@ole
ole / LayoutCallAsFunction.swift
Created Jun 12, 2022
SwiftUI: How any `Layout`-conforming type becomes a container view when it's used with a `@ViewBuilder` closure.
View LayoutCallAsFunction.swift
// Excerpt from `SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64-apple-ios.swiftinterface`
// in Xcode 14.0b1
//
// This is how any `Layout`-conforming type becomes a container view
// when it's used with a `@ViewBuilder` closure.
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
extension SwiftUI.Layout {
@_alwaysEmitIntoClient public func callAsFunction<V>(@SwiftUI.ViewBuilder _ content: () -> V) -> some SwiftUI.View where V : SwiftUI.View {
return _VariadicView.Tree(
root: _LayoutRoot(self), content: content())
View BaseSDKVersionCheck.swift
// 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
Add links in SwiftUI Text views via a custom string interpolation.
View Text+Link.swift
@ole
ole / MyMainActor.swift
Last active May 20, 2022
A reimplementation of the basics of MainActor. Sample code for https://oleb.net/2022/how-mainactor-works/
View MyMainActor.swift
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 12, 2022
Code for my article "A heterogeneous dictionary with strong types in Swift" https://oleb.net/2022/heterogeneous-dictionary/
View HeterogeneousDictionary.swift
// 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 Dec 23, 2021
Using CheckedContinuation to communicate between concurrent tasks.
View TaskSignaling.swift
// 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 Dec 23, 2021
Using CheckedContinuation to communicate between two concurrent tasks.
View Channel.swift
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.
View FlipView.swift
// 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 Dec 2, 2021
Weird SwiftUI two-column NavigationView behavior
View TwoColumnNavigation.swift
// 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
View HStack-VStack-dynamic-switching.swift
import SwiftUI
struct ContentView: View {
@State var horizontal: Bool = true
@Namespace var namespace
var body: some View {
VStack(spacing: 40) {
if horizontal {
HStack { items }