|
Hi all, I'm halfway through 100daysOfSwiftUI and started working on my own app (maybe too early but learned a lot already by trying ;) I got following issue now : I have a class with user preferences which can be stored in UserDefaults :
I have a View that is shown in a sheet to fill in the User Preferences :
My ContentView has 2 @ObservedObject variables : userPrefs and info. It also shows the sheet when a button is pressed :
InfoData is another class where data is being read from an API. It has a base URL to access the API and I want to add the apiKey from the userPrefs to that base URL.
I fail to see however how I can read that apiKey value in that class... If it was a view instead of a class, I could use a @ObservedObject var userPrefs: UserPreferences in that View and do something like InfoData(userPrefs: self.userPrefs) in my ContentView. However, it's a class, not a View and if I try to write following in my ContentView, I get an error :
--> Cannot use instance member 'userPrefs' within property initializer; property initializers run before 'self' is available So I'm not sure how I can use that apiKey from the UserPreferences in my InfoData class... |
|
|
|
sorry, i should have explained the InfoData better : it's an @ObservableObject class because it loads data from an api and provides that data (@Published variables) to the ContentView. The function that calls the URL is a method in the InfoData class and the whole issue is exactly that i cannot access userPrefs.prefs.apiKey from there because that class doesnt know 'userPrefs'. |
|
|
|
Fixed it by looking through the code of Paul his ControlRoom app on Github : https://github.com/twostraws/ControlRoom Made UserPreferences an @EnvironmentObject so its data can be shared with all Views and also with the InfoData class. This article gives more info : https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views Basically, in SceneDelegate.swift I added following :
and passed these as environmentObjects to ContentView. In any View I can now use :
and in my InfoData class I can now finally use the userPrefs also :
If the user would change the apiKey in the preferences, this change is automatically visible in all the Views and in my InfoData class :) Learned a lot researching this. Also looking at the code of ControlRoom teached me a lot (although it's way over my current level, it doesn't seem to be pure SwiftUI, also lot of UIKit (and Combine)). |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.