One of the biggest advantages of a strong type system is that carefully chosen types can make illegal states unrepresentable in your programs. Many Cocoa APIs don’t take full advantage of this because they aren’t optimized for Swift.| Ole Begemann
The Swift website provides nightly builds of the Swift compiler (called toolchains) for download. Building with a nightly compiler can be useful if you want to check if a bug has already been fixed on main, or if you want to experiment with upcoming language features such as Embedded Swift, as I’ve been doing lately.| Ole Begemann
I found the place in the compiler’s source code where it applies a special attribute to DispatchQueue.main.async closures. Plus, a rant about how Xcode presents Swift concurrency context to the programmer.| Ole Begemann
iOS 16 beta 4 is the first SDK release that supports Live Activities. A Live Activity is a widget-like view an app can place on your lock screen and update in real time. Examples where this can be useful include live sports scores or train departure times.| Ole Begemann
@MainActor is a Swift annotation to coerce a function to always run on the main thread and to enable the compiler to verify this. How does this work? In this article, I’m going to reimplement @MainActor in a slightly simplified form for illustration purposes, mainly to show how little “magic” there is to it. The code of the real implementation in the Swift standard library is available in the Swift repository.| Ole Begemann
The relative sizing modifier interacts in an interesting way with stack views and other containers that distribute the available space among their children. Understanding why can teach us a lot about SwiftUI’s layout algorithm.| Ole Begemann
A SwiftUI layout and view modifier for relative sizing. “Make this view 50 % of the width of its container”.| Ole Begemann
A shell script that assigns a keyboard shortcut to the Unmodified Originals menu command in Photos on the Mac.| Ole Begemann
I fixed Karoy Lorentey’s swift-evolution workflow for Alfred.| Ole Begemann
Foundation overloads the pattern matching operator ~= to enable matching against error codes in catch clauses.| Ole Begemann
Double Fine Adventure is a 12.5-hour documentary about the development of the game Broken Age. I loved it.| Ole Begemann
I wrote an app for observing how various SwiftUI constructs and container views affect view lifecycles, including the lifetime of state.| Ole Begemann
The clipped() modifier in SwiftUI doesn’t limit hit testing to the clipping region. The clipped view can still receive taps/clicks outside the visible area. Add .contentShape(Rectangle()) to change this behavior.| Ole Begemann
SwiftUI views and view modifiers can be divided into “rendering“ and “non-rendering” groups. The position of the `animation` modifier with respect to non-rendering modifiers is irrelevant for the question if a change gets animated or not.| Ole Begemann
The fifth edition of our book Advanced Swift, fully updated for Swift 5.6, is out now.| Ole Begemann
By convention, functions should respond to cancellation by throwing a specfic error. But not all functions follow the convention.| Ole Begemann
Mac apps built with Xcode 14.0 and 14.0.1 may contain concurrency bugs because the Swift 5.7 compiler can generate invalid code when targeting the macOS 12 SDK. If you distribute Mac apps, you should build them with Xcode 13.4.1 until Xcode 14.1 is released.| Ole Begemann
If you call SwiftUI’s task modifier inside a view’s body property, the async operation will run on the main actor. However, this isn’t the case if you call task from a helper property or function that isn’t main-actor-annotated. Why?| Ole Begemann
Don’t store string indices or offsets if possible. They aren’t stable over time or across runtime environments.| Ole Begemann
I wrote a dictionary type where each key defines the type of value it stores. The public API is fully type-safe.| Ole Begemann
Synchronous functions can perform the same cancellation checks as async code. Adding cancellation support can improve response times when async code calls long-running non-async code.| Ole Begemann