Skip to content

Instantly share code, notes, and snippets.

@ozalexo
Created October 18, 2019 23:57
Show Gist options
  • Save ozalexo/b9b79ab266efe6fa8691d45d8b7aa495 to your computer and use it in GitHub Desktop.
Save ozalexo/b9b79ab266efe6fa8691d45d8b7aa495 to your computer and use it in GitHub Desktop.
SwiftUI: Difference between computed property and view as property
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