Great series of short articles introducing Apple's Metal framework.
- 2022-04-01: Day 1: Devices
- 2022-04-02: Day 2: Buffers
- 2022-04-03: Day 3: Commands
- 2022-04-04: Day 4: MTKView
- 2022-04-05: Day 5: Shaders
- 2022-04-06: Day 6: Pipelines
// Make sure both the string and the substring are larger than 15 UTF-8 bytes | |
// to avoid the small string optimization | |
var str = "Hello world 1 Hello world 2 Hello world 3 Hello world 4 Hello world 5 Hello world 6 Hello world 7 Hello world 8 Hello world 9" | |
let prefixToStrip = 14 | |
var substr = str.dropFirst(prefixToStrip).prefix(27) | |
let strToAppend = "+++APPENDED+++" | |
// ⚠️ It makes a difference how you mutate the Substring: | |
// - substr.append → Apparently makes a copy of the entire original string | |
// *and* even shifts the original string contents back to make room, |
#!/bin/zsh | |
# Test if the Swift compiler knows about a particular language feature. | |
# | |
# Usage: | |
# | |
# swift-has-feature [--swift SWIFT_PATH] [--language-version LANGUAGE_VERSION] FEATURE | |
# | |
# The feature should be an upcoming or experimental language feature, | |
# such as `"StrictConcurrency"` or `"ExistentialAny"`. |
import SwiftUI | |
@main | |
struct GroupWithTaskApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} |
import Foundation | |
/// An "extension" of FloatingPointFormatStyle that adds a `minusSign` API to customize | |
/// the character(s) used as the minus sign. | |
/// | |
/// You could add additional extensions by following the same pattern. | |
/// | |
/// All other APIs are copied from FloatingPointFormatStyle and forward to it for their | |
/// implementation. This isn’t a full replica of the FloatingPointFormatStyle API, though, | |
/// because it’s only intended as a proof of concept. |
import Foundation | |
extension FormatStyle { | |
static func ordinal<FormatInput: BinaryInteger>() -> OrdinalFormatStyle<FormatInput> | |
where Self == OrdinalFormatStyle<FormatInput> | |
{ | |
OrdinalFormatStyle() | |
} | |
} |
Great series of short articles introducing Apple's Metal framework.
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
VStack { | |
FrameworkView1 { | |
Text("FrameworkView1") | |
} | |
.border(.red) |
// UserDefaults KVO observation with AsyncSequence/AsyncStream | |
// Ole Begemann, 2023-04 | |
// Updated for Swift 6, 2024-11 | |
// https://gist.github.com/ole/fc5c1f4c763d28d9ba70940512e81916 | |
import Foundation | |
// This is ugly, but UserDefaults is documented to be thread-safe, so this | |
// should be OK. | |
extension UserDefaults: @retroactive @unchecked Sendable {} |
import SwiftUI | |
extension View { | |
/// Proposes a percentage of its received proposed size to `self`. | |
/// | |
/// This modifier multiplies the proposed size it receives from its parent | |
/// with the given factors for width and height. | |
/// | |
/// If the parent proposes `nil` or `.infinity` to us in any dimension, | |
/// we’ll forward these values to our child view unchanged. |
struct MyError: Error {} | |
func fireAndForget() async { | |
await withThrowingTaskGroup(of: Void.self) { group in | |
group.addTask { | |
print("child task start") | |
print("child task throws") | |
throw MyError() | |
} | |
// Notice that we're not awaiting the child task. |