Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
91: State and Binding Property Wrappers
If you've been playing around with SwiftUI, you've probably noticed that the UI doesn't just "change" because you changed a variable. SwiftUI needs to know when to redraw the screen, and that's where @State and @Binding come in. I like to think of these as the "source of truth" and the "remote control."
Let's build a simple User Profile Editor. We want a main screen that displays a username and a separate, smaller component that lets us edit that name. This is the perfect way to see how data flows between views.
Creating the Source of Truth
First, I'll set up the main view. Since the username is something that changes and should trigger a UI update, I can't just use a standard var. I need @State. This tells SwiftUI: "Keep an eye on this value; if it changes, refresh any part of the UI that depends on it."
struct ProfileView: View {
@State private var username: String = "SwiftLearner_99"
var body: some View {
VStack(spacing: 20) {
Text("Current User: \(username)")
.font(.title)
// We'll put our editor component here in a moment
UsernameEditorView(name: username)
}
.padding()
}
}
The "Read-Only" Mistake
Now, I want to move the TextField into its own view called UsernameEditorView to keep things clean. My first instinct is usually to just pass the string along. Let's see what happens when I do that:
struct UsernameEditorView: View {
var name: String // Just a regular property
var body: some View {
TextField("Enter name", text: .constant(name))
.textFieldStyle(.roundedBorder)
}
}
Here's the problem: this doesn't actually work. The name property in UsernameEditorView is a constant. Even if I could change it, it's just a copy of the string from the parent. The parent ProfileView would never know the child changed anything, and the UI wouldn't update. I've essentially created a "read-only" view.
Creating a Two-Way Connection with Binding
To fix this, I need to stop passing a value and start passing a reference. That's what @Binding is for. It doesn't hold its own data; it just points back to the @State variable in the parent. It's like giving the child view a remote control to the parent's data.
Let's rewrite the editor:
struct UsernameEditorView: View {
@Binding var name: String // Now it's a binding!
var body: some View {
TextField("Enter name", text: $name)
.textFieldStyle(.roundedBorder)
}
}
Notice the $name in the TextField. That dollar sign is critical. In SwiftUI, adding the $ prefix accesses the "projected value," which for a @State or @Binding property, is the binding itself. It allows the TextField to write changes directly back to the source.
Wiring it all together
Now I have to go back to my ProfileView. Since UsernameEditorView now expects a binding, I can't just pass username. I have to pass $username.
struct ProfileView: View {
@State private var username: String = "SwiftLearner_99"
var body: some View {
VStack(spacing: 20) {
Text("Current User: \(username)")
.font(.title)
// Pass the binding using the $ prefix
UsernameEditorView(name: $username)
}
.padding()
}
}
Now, when you type in the TextField, the @Binding updates the @State in the parent, which in turn triggers the Text view to redraw instantly. It's a clean, circular flow of data that keeps your UI in sync without you having to manually call "refresh" methods.
📋 Practical Task
Exercise: Build a Notification Preference Panel
Your task is to create a settings interface that allows a user to toggle their notification preferences.
- Create a parent view called
NotificationSettingsView. - Inside the parent, create a
@Stateboolean variable calledisNotificationsEnabled. - Create a child view called
NotificationToggleView. - The child view should use
@Bindingto accept the boolean value. - In the child view, implement a
Togglecomponent that is bound to that value. - In the parent view, display a
Textlabel that says "Notifications are ON" or "Notifications are OFF" based on the current state, and place theNotificationToggleViewbelow it.
There are no comments for now.