@State property in SwiftUI

Property Wrappers?

You can think of property wrappers as additional behaviours and functionalities added to a property to do more. You can think of them as weak or @IBOutlet but more powerful.

What is it?

It looks something like this:

@State private var iLovePizza: Bool = true
@State private var iLovePizza: Bool = truevar body: some View {  VStack {
if iLovePizza {
Text("I Love Pizza 🍕")
} else {
Text("🍕 = Nada")
}

Button("Toggle") {
iLovePizza.toggle()
}
}
}

Binding

@State private var name: String = ""var body: some View {
VStack(alignment: .leading) {
Text("My Name is \(name)")
TextField("Enter your name", text: $name)
}.padding()
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melvin John

Melvin John

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC