Post

Replies

Boosts

Views

Activity

Set value to @State in init function
I want to pass a value to a property marked with @State, and it doesn't work. struct AView: View { @State private var content: String? var body: some View { Text(content ? "") } init(content: String) { self.content = content } } #Preview { AView(content: "Test") } But if I change the content to non optional, it works well. struct AView: View { @State private var content: String var body: some View { Text(content) } init(content: String) { self.content = content } } #Preview { AView(content: "Test") } I googled for a while and found that I need to use a spacial way to initialize @State property in the first case. init(content: String) { _content = State(initialValue: content) } It works, but why? Is there any difficulty to unified API between optional and non optional @State property?
0
0
467
Dec ’23