Skip to content

Instantly share code, notes, and snippets.

@filimo
Created December 18, 2019 19:57
Show Gist options
  • Save filimo/c2552bd7c3f377040681271e6f4ae5ea to your computer and use it in GitHub Desktop.
Save filimo/c2552bd7c3f377040681271e6f4ae5ea to your computer and use it in GitHub Desktop.
How to separate Global Store
import SwiftUI
import PlaygroundSupport
class GlobalStore: ObservableObject {
static let shared = GlobalStore()
private init() {}
@Published var api1 = Api1Store()
@Published var api2 = Api1Store()
}
class Api1Store {
var field = ""
}
class Api2Store {
var field = ""
}
struct Test: View {
@ObservedObject var store = GlobalStore.shared
var body: some View {
VStack {
TextField("api1 field", text: $store.api1.field)
TextField("api1 field", text: $store.api1.field)
Divider()
TextField("api2 field", text: $store.api2.field)
TextField("api2 field", text: $store.api2.field)
}
}
}
let viewController = UIHostingController(rootView: Test())
PlaygroundPage.current.liveView = viewController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment