Created
December 19, 2024 15:20
-
-
Save VAndrJ/91a26f1d1314aa62e591b53180afce9f to your computer and use it in GitHub Desktop.
Observable class naming issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| import Observation | |
| struct ContentView: View { | |
| @State var member = Member(name: "Swift") | |
| var body: some View { | |
| VStack(spacing: 24) { | |
| Text("\(member.name)") | |
| Text("\(member.subscription ? "Member" : "Guest")") | |
| .foregroundStyle(member.subscription ? .green : .red) | |
| Button("Toggle subscription") { | |
| member.subscription.toggle() | |
| } | |
| } | |
| } | |
| } | |
| @Observable | |
| class Member { | |
| let name: String | |
| var subscription: Bool | |
| init( | |
| name: String, | |
| subscription: Bool = false | |
| ) { | |
| self.name = name | |
| self.subscription = subscription | |
| } | |
| internal nonisolated func access<_TObservableRegistrarMember>( | |
| keyPath: KeyPath<Member, _TObservableRegistrarMember> | |
| ) { | |
| _$observationRegistrar.access(self, keyPath: keyPath) | |
| } | |
| internal nonisolated func withMutation<_TObservableRegistrarMember, _TObservableMutationResult>( | |
| keyPath: KeyPath<Member, _TObservableRegistrarMember>, | |
| _ mutation: () throws -> _TObservableMutationResult | |
| ) rethrows -> _TObservableMutationResult { | |
| try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation) | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment