Created
July 24, 2021 15:18
-
-
Save couchdeveloper/ebaff1befa9ad2b0210366c4889393b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
// Demonstrates issue when assigning `@State` values in the initialiser | |
struct ContentView: View { | |
@State var string: String = "Initial" // default initialise the value | |
init(string: String) { | |
self.string = string // assign it again with value `string` | |
// We expect that `self.string` has been assigned the value `string`. | |
// However, due to how SwiftUI manages the state store, this is not always the case! | |
print("init: \(string), self.string: \(self.string)") | |
} | |
var body: some View { | |
return Text(string) | |
} | |
} | |
import PlaygroundSupport | |
// We expect that the view renders "Hello World!", but it renders "Initial" | |
PlaygroundPage.current.setLiveView(ContentView(string: "Hello World!")) |
Author
couchdeveloper
commented
Jul 24, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment