Question
I have the following ObservableObject and SwiftUI view.
class ExampleState: ObservableObject {}
struct ExampleView: View {
@StateObject var state = ExampleState()
var body: some View {
EmptyView()
}
}
And I want to pass state to the view from a UIKit view.
class ExampleViewController: UIViewController {
var state = ExampleState()
func test() -> some View {
ExampleView(state: state) // A compile error occurs on this line
}
}
But I got compile error on initializer calling. And the error message says "Reference to property 'state' in closure requires explicit use of 'self' to make capture semantics explicit"
First, I do not understand why this is closure.
Second, when I followed the error message and put self, a memory leak occurred. So could you tell me if I can make it weak reference.
Environment
- iOS 16
- Xcode 14.3.1