Created
October 18, 2019 23:57
-
-
Save ozalexo/b9b79ab266efe6fa8691d45d8b7aa495 to your computer and use it in GitHub Desktop.
SwiftUI: Difference between computed property and view as property
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 | |
struct ContentView: View { | |
@State private var name = "John" | |
@State private var surename = "Connor" | |
var motto1: some View { | |
VStack { | |
TextField("Enter your name", text: $name) | |
Text("Hello, \(name)!") | |
}.padding() | |
} | |
/* Will not work with the following error | |
Cannot use instance member '$surename' within property initializer; property initializers run before 'self' is available | |
*/ | |
// var motto2 = VStack { | |
// TextField("Enter your surename", text: $surename) | |
// Text("I'll terminate you, \(surename)!") | |
// }.padding() | |
var body: some View { | |
motto1 | |
.foregroundColor(.red) | |
// motto2 | |
// .foregroundColor(.blue) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment