Skip to content

Instantly share code, notes, and snippets.

// SwiftUI question: How do you put a continuous background behind a GridRow?
//
// I would have thought:
//
// GridRow { … }
// .background { … }
//
// But this puts the background behind each cell individually (behaves like Group),
// which is not what I want.
@ole
ole / Stateful.swift
Last active August 11, 2024 00:21
A wrapper view that provides a mutable Binding to its content closure. Useful in Xcode Previews for interactive previews of views that take a Binding. https://twitter.com/olebegemann/status/1565707085849010176
import SwiftUI
/// A wrapper view that provides a mutable Binding to its content closure.
///
/// Useful in Xcode Previews for interactive previews of views that take a Binding.
struct Stateful<Value, Content: View>: View {
var content: (Binding<Value>) -> Content
@State private var state: Value
init(initialState: Value, @ViewBuilder content: @escaping (Binding<Value>) -> Content) {
@ole
ole / _StackLayoutCache.swift
Last active October 19, 2024 01:53
Structure of _StackLayoutCache and related types, used by SwiftUI as the Cache type for VStackLayout and HStackLayout
// Structure of _StackLayoutCache and related types.
// Used by SwiftUI as the Cache type for VStackLayout and HStackLayout.
//
// As of: iOS 16.0 simulator in Xcode 14.0b6
import SwiftUI
struct _StackLayoutCache {
var stack: StackLayout
}
@ole
ole / LayoutCallAsFunction.swift
Created June 12, 2022 15:19
SwiftUI: How any `Layout`-conforming type becomes a container view when it's used with a `@ViewBuilder` closure.
// 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())
// 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 January 23, 2025 20:54
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 August 11, 2024 00:21
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 August 11, 2024 00:21
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.