This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension LocalizedStringKey.StringInterpolation { | |
/// String interpolation support for links in Text. | |
/// | |
/// Usage: | |
/// | |
/// let url: URL = … | |
/// Text("\("Link title", url: url)") | |
mutating func appendInterpolation(_ linkTitle: String, link url: URL) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dispatch | |
@globalActor | |
final actor MyMainActor { | |
// Don’t allow others to create instances | |
private init() {} | |
// Requirements from the implicit GlobalActor conformance | |
typealias ActorType = MyMainActor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State var horizontal: Bool = true | |
@Namespace var namespace | |
var body: some View { | |
VStack(spacing: 40) { | |
if horizontal { | |
HStack { items } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct ContainerEncoded: Encodable { | |
var date: Date = .now | |
func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
try container.encode(date) | |
} | |
} |