SwiftUI declarative UI with state management
Sofia Martinez
Jan 2026
2 tabs
import SwiftUI
struct ContentView: View {
@State private var username = ""
@State private var isLoggedIn = false
@StateObject private var viewModel = LoginViewModel()
var body: some View {
if isLoggedIn {
HomeView(username: username)
} else {
LoginForm(
username: $username,
isLoggedIn: $isLoggedIn,
viewModel: viewModel
)
}
}
}
struct LoginForm: View {
@Binding var username: String
@Binding var isLoggedIn: Bool
@ObservedObject var viewModel: LoginViewModel
var body: some View {
VStack(spacing: 20) {
TextField("Username", text: $username)
.textFieldStyle(RoundedBorderTextFieldStyle())
.autocapitalization(.none)
SecureField("Password", text: $viewModel.password)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button("Login") {
viewModel.login(username: username) { success in
isLoggedIn = success
}
}
.disabled(username.isEmpty || viewModel.password.isEmpty)
if viewModel.isLoading {
ProgressView()
}
if let error = viewModel.errorMessage {
Text(error)
.foregroundColor(.red)
.font(.caption)
}
}
.padding()
}
}
import Foundation
import Combine
class LoginViewModel: ObservableObject {
@Published var password = ""
@Published var isLoading = false
@Published var errorMessage: String?
private var cancellables = Set<AnyCancellable>()
func login(username: String, completion: @escaping (Bool) -> Void) {
isLoading = true
errorMessage = nil
// Simulate API call
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.isLoading = false
if username.isEmpty || self.password.isEmpty {
self.errorMessage = "Invalid credentials"
completion(false)
} else {
completion(true)
}
}
}
}
2 files · swift
Explain with highlit
SwiftUI revolutionizes iOS development with declarative syntax where you describe what the UI should look like based on state. Views automatically update when state changes using property wrappers like @State, @Binding, and @ObservedObject. The @State wrapper creates source of truth for simple value types, while @Binding creates two-way connections to pass state down the view hierarchy. For complex state shared across views, I use ObservableObject with @Published properties. SwiftUI's runtime efficiently diffs and updates only what changed, eliminating manual UI updates. This reactive approach reduces bugs and makes UI code more predictable and testable.