Skip to content

Instantly share code, notes, and snippets.

@jhowlin
Last active February 17, 2025 01:51
Show Gist options
  • Save jhowlin/a8cef0597a4bfdd03ea9a51c364e40ad to your computer and use it in GitHub Desktop.
Save jhowlin/a8cef0597a4bfdd03ea9a51c364e40ad to your computer and use it in GitHub Desktop.
@State with type @observable
@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