Binding a TextField to State
Connect a text field to a @State string.
Binding a TextField to State is a free SwiftUI Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet TextField
A TextField is SwiftUI's box where users type. Give it a prompt and somewhere to store what they enter, and you have live text input. ✍️
Text Needs a Home
Typed characters must go somewhere. You store them in a @State string that the text field reads from and writes to as the user types.
@State private var name = ""The Binding Connection
A TextField needs a two-way link called a binding. It both reads the current value and writes new keystrokes back into your state.
The Dollar Sign
To pass a binding, prefix the property with $. Writing $name hands the text field a two-way connection instead of a plain copy.
TextField("Name", text: $name)The Placeholder
The first argument is the placeholder: faint hint text shown when the field is empty. It disappears the moment the user starts typing.
TextField("Enter your name", text: $name)Reading the Value
Because the field writes into your state, you can read name anywhere in the body to react to what the user has typed so far.
Text("Hello, \(name)")A Bordered Look
Add the textFieldStyle modifier with .roundedBorder to give the field a clean, tappable outline instead of a bare line.
TextField("Name", text: $name)
.textFieldStyle(.roundedBorder)Live Updates Are Free
You never write code to refresh the screen. When the binding changes the state, SwiftUI re-renders the view automatically for you. ✨
One Source of Truth
The @State string is the single source of truth. The field and any Text that reads it always stay perfectly in sync.
Plain Value vs Binding
Writing name passes a read-only copy. Writing $name passes the binding the field needs to send edits back. The dollar sign matters.
Putting It Together
A state string, a TextField bound with $, and a Text that reads it: that is a complete, reactive input screen.
TextField("Name", text: $name)
Text("Hi, \(name)")Quick Check
How do you give a TextField a two-way link to a @State string?
Recap
You bound a TextField to @State with the $ prefix, so typing updates your value and the UI refreshes itself. That is reactive input in a nutshell. 🎉
Frequently asked questions
Is the “Binding a TextField to State” lesson free?
Yes — the full text of “Binding a TextField to State” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Binding a TextField to State”?
Connect a text field to a @State string. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Binding a TextField to State” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Binding a TextField to State
- Keyboard Types & Placeholders
- SecureField for Passwords
- Live Input Validation