History
Loading...
Loading...
September 26, 2025
@StateObject and ObservableObject classes with the new @Observable macro. Simply add @Observable to your class and remove @Published wrappers - properties are automatically observable. Use @State instead of @StateObject in your views for cleaner, more performant code.The @Observable macro eliminates boilerplate code by automatically making all properties observable without @Published wrappers. It provides better performance through fine-grained observation and reduces memory overhead compared to Combine-based ObservableObject.
@Observable
class UserProfile {
var name: String = ""
var email: String = ""
var isLoading: Bool = false
func updateProfile() {
isLoading = true
// API call logic
isLoading = false
}
}
struct ProfileView: View {
@State private var profile = UserProfile()
var body: some View {
Form {
TextField("Name", text: $profile.name)
TextField("Email", text: $profile.email)
if profile.isLoading {
ProgressView()
}
}
}
}This approach reduces cognitive load by removing the need to decide which properties need @Published, prevents common bugs from forgetting @Published annotations, and offers improved performance since SwiftUI only updates views when specific observed properties change rather than when any published property updates.