SwiftUWhy
Welcome to my new series on things I don’t understand about Apple’s premier user interface framework.
[…]
To be clear, these are things I don’t understand, not necessarily things that are “wrong.” They sure look wrong (or at least “suboptimal”) to me! But maybe there are good reasons, and I just don’t know them yet. SwiftUI experts and historians, please tell me!
Previously:
Update (2025-01-10): John Siracusa (GitHub):
Why is there (apparently?) no
Picker
style on macOS that can be configured with a fixed width, likeNSPopUpButton
in AppKit?For example, consider a text field for numeric input followed by a picker for units, both aligned to the right side of a System Settings-style Form. Each time you pick a different unit, the
Picker
changes width and moves the text field.
Q: Why is Xcode no longer able to render a preview of my SwiftUI view?
A: Sometimes, the answer is that you’ve got a call to a log method on a
Logger
object in a code path that’s executed when rendering a preview but rarely or not at all otherwise, and this call passes a string without any values interpolated into it (e.g.,logger.log("Hello")
). This causes an error, and this error is only visible in the big spew of output available through the stethoscope icon in the preview pane.
ScrollView
on macOS includes default support for swipe gestures and mouse scroll wheels, but not page up/down, home/end, or arrow keys?
See also: the Mastodon tag.
Update (2025-01-17): John Siracusa:
Why can’t I find a simple way to do this?
.textSelection(allowSelection ? .enabled : .disabled)
Update (2025-02-11): John Siracusa:
Xcode should track the lifetime number of hours spent trying to find the error in your SwiftUI view code that is causing the “compiler is unable to type-check this expression in reasonable time” message to appear after the obligatory a 60-second wait.
To be clear, this message is almost never (directly?) related to type checking when it happens in a SwiftUI View. Instead, it’s caused by things like a typo in the parameter name of a method call.
“A typo in a parameter name? Why, that should be pinpointed instantly by the complier!” Sure, in normal code. But in a SwiftUI view, you get to try to find it yourself, with no help! Each try costs you 60 seconds.