WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Binding state to user interface controls

Forums > 100 Days of SwiftUI

The problem is that Swift differentiates between “show the value of this property here” and “show the value of this property here, but write any changes back to the property.”

In the case of our text field, Swift needs to make sure whatever is in the text is also in the name property, so that it can fulfill its promise that our views are a function of their state – that everything the user can see is just the visible representation of the structs and properties in our code.

This is what’s called a two-way binding: we bind the text field so that it shows the value of our property, but we also bind it so that any changes to the text field also update the property.

struct ContentView: View {
    @State private var name = ""

    var body: some View {
        Form {
            TextField("Enter your name", text: $name)
            Text("Hello World")
        }
    }
}

I dont understand this differentiates.

In this sentences “show the value of this property here” does that mean only assign the value once?

ın second sentences “show the value of this property here, but write any changes back to the property.” does that mean assign the value to property but if there is a change so change the value and assign the property?

ı stuck in this section. I dont understand totally

1      

“show the value of this property here” means to just display the property's current value. In this example, that would simply be name.

“show the value of this property here, but write any changes back to the property.” means to display the property's current value and allow the user to make changes to the property. That is done with a binding: $name.

In the case of a TextField, you want to show the property's current value but also update the property's value when the user types in the field. So you use a binding to the property like $name.

1      

a two way binding means that if you were to update the name it would be shown in the TextEdit ( for instance if you do that onAppear or on the declaration) but also that if that as you chage the text it changes the name value.

1      

"Differentiates" basically means "Recognizes that two things are different from each other."

He is saying that Swift recognizes that name and $name have two different meanings.

name means “show the value of the name property here”

$name means “show the value of the name property here, but also save any changes written in the text field back to the name property.”

So, if you write this code, the value currently stored in the name property will be shown in the TextField.

struct ContentView: View {
    @State private var name = ""

    var body: some View {
        Form {
            TextField("Enter your name", text: name)
        }
    }
}

But if you write this code, the value currently stored in the name property will be shown in the TextField, and any new text written into the TextField will replace what is currently stored in the name property.

struct ContentView: View {
    @State private var name = ""

    var body: some View {
        Form {
            TextField("Enter your name", text: $name)
        }
    }
}

1      

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.