Last active
February 17, 2025 01:51
-
-
Save jhowlin/a8cef0597a4bfdd03ea9a51c364e40ad to your computer and use it in GitHub Desktop.
@State with type @observable
This file contains 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
@main | |
struct SomeApp: App { | |
@State var path: [Int] = [] | |
var body: some Scene { | |
return WindowGroup { | |
NavigationStack(path: $path) { | |
Button("Push") { | |
path.append(1) | |
}.navigationDestination(for: Int.self) { _ in | |
SomeView() | |
} | |
} | |
} | |
} | |
} | |
@Observable | |
class SomeManager { | |
init () { | |
print("Manager init") | |
} | |
deinit { | |
print("Manager deinit") | |
} | |
var test = "Hello" | |
} | |
struct SomeView: View { | |
@State var manager = SomeManager() | |
init() { | |
print("Some view init") | |
} | |
var body: some View { | |
Button(manager.test) { | |
manager.test = UUID().uuidString | |
} | |
} | |
} | |
// Tapping the "Push" button outputs | |
// Manager init | |
// Some view init | |
// Tapping "Test" replaces text with UUID string | |
// Tapping back: | |
// Manager deinit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment