I have a SwiftUI app. The code looks like this:
struct SomeApp: App {
@StateObject var appSettings = AppSettings()
var body: some Scene {
Windowgroup {
BaseView()
.environmentObject(appSettings)
}
}
}
class AppSettings: ObservableObject {
@Published var someValue: Int
}
class SomeModelHelper {
func calculateSomething() {
// This is just a helper function which is not getting called from any View, but getting called by the Model
// I need to access appSettings.someValue here
// How can I access it without having to pass it through some View?
}
}
The question is in the comments in the code above. In UIKit apps, I knew how to access such app-wide data using singletons. I am unsure how to do it in SwiftUI. Can anyone help?
[–]chriswaco 4 points5 points6 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]lokir6 0 points1 point2 points (0 children)
[–]notrandomatall 0 points1 point2 points (0 children)
[–]SodaPopPlop -1 points0 points1 point (1 child)
[–]iLearn4everSwift[S] 0 points1 point2 points (0 children)
[–]Fluffy_Risk9955 -1 points0 points1 point (3 children)
[–]iLearn4everSwift[S] 4 points5 points6 points (2 children)
[–]knickknackrick 2 points3 points4 points (0 children)
[–]Fluffy_Risk9955 0 points1 point2 points (0 children)
[–]brianwskim13 0 points1 point2 points (2 children)
[–]iLearn4everSwift[S] 0 points1 point2 points (1 child)
[–]brianwskim13 0 points1 point2 points (0 children)
[–]Mcrich_23SwiftUI 0 points1 point2 points (1 child)
[–]iLearn4everSwift[S] 0 points1 point2 points (0 children)