WWDC23 SwiftUI Q&A

Khoa Pham
22 min readJul 4, 2023

Interesting SwiftUI Q&A during WWDC23

Observable vs ObservableObject

Q: With the new SwiftUI @observable macro, are there any cases where ObservableObject would still be a better alternative?

A: Use ObservableObject when you need to back deploy to before iOS 17

A: SwiftUI registers dependencies is a value is read during the evaluation of body. Indirect modifications should invalidate the dependencies.

containerRelativeFrame in ScrollView

Q: For the containerRelativeFrame, does that work cleanly in a List as well as a ScrollView?

A: Yes, a List establishes a container relative frame like ScrollView. You can see the docs for this modifier for more examples of what other views establish a container relative frame:

Inject ViewModel as StateObject

Q: The most perfomant way i found to do injection of viewModels while keeping the @StateObject performance is to use this specific syntax. Is there a better way of achieving property injection into state objects?

class Bar {}
class FooVM: ObservableObject {
let externalDependency: Bar
init(externalDependency: Bar) {
self.externalDependency = externalDependency
}
}
struct FooView: View {
@StateObject private var viewModel: FooVM

--

--