Stopping SetNSColor(CGContext*, void const*) Crashes in UIKit

TL;DR I recently encountered a crash on SetNSColor(CGContext*, void const*) with [__SwiftValue CGColor]: unrecognized selector sent to instance. It took a while to find the root cause as the stack trace lacked a lot of detail. Ultimately it was a SwiftUI Color in a UIKit API that expects a UIColor (or raw CGColor). In this case I passed a SwiftUI Color via the .foregroundColor attribute of an NSAttributedString that UIKit then rendered. ...

How to use print statements in SwiftUI

Debugging SwiftUI views can feel a little challenging due to its declarative nature. In this post, we’ll explore how to add print statements into your SwiftUI code to observe state changes, view updates, and data flow. Why use print in SwiftUI? SwiftUI’s declarative paradigm means you describe what your UI should look like, and the framework handles when to update it. This can make it easier to reason about but harder to debug lifecycle events and UI-related state changes. print statements give you: ...