|
Hi there! I'm new to swift but have experience coding in other languages, mostly java. I've looked through many of the different tutorials available at this site, and even though I'm starting to get used to some of the quirks of swift and swiftui there are still some things I'm having trouble to wrap my head around, like how to use the properties correctly for example. Here's what I'm struggling with currently and would like some help with: I have made a MainView that downloads a JSON from a website (redacted here since I don't want it publicly available) and decode it through the use of a Codable class object and use parts of it to populate a list, so far so good. I'm now trying to make that list of objects available to other views and classes using ObservableObjects but haven't managed so far. The simplest solution seemed to be (at least to me) making a separate ObservableObject class that just contains an array of my Codable objects, but I haven't been able to make it work so far. This data will later be stored in a database, but I've tried to just focus on the GUI first and will focus on building the database and all it's interactions later on, since I figured I should try to understand how this property business works. But maybe building up the database and all it's interactions would solve this issue? Here's my working code: "Main View"
"ConfigurationVO"
Then I tried making a new class "ConfigurationList" import Foundation
and changed
in MainView to
but that created the following issues that I haven't been able to solve yet:
I tried to solve it using the $-operator, but that's another quirk I haven't fully grasped yet. Am I on the right track or totally of the rails? If so how would I go about doing this? |
|
hi, the syntax may be confusing you because of your name choices ... you have a variable so to refer to the list of perhaps you should be writing
and when updating the UI after receiving a valid response you should write
hope that helps, DMG |
|
Hi delawaremathguy, Thank you for the help, you were correct! When you pointed that out I realized my mistake referencing the list of objects instead of the objects themselves. Such a simple mistake and I didn't even see it because I was so focused on understanding the properties... But why are you recommending @StateObject instead of @ObservedObject? I understand that it has to do with ownership of the object, but I'm not sure exactly how that works. I want to be able to refence this list from at least two other views, and since I wanted the list to live regardless of which view was active I thought it was better that none of them "owned" it. But what happens when there are no @StateObjects or if I have multiple @StateObject? |
|
hi,
i'll refer you to this article by Paul Hudson about this iOS 14 property wrapper.
i suspect you'll now want to place this object of type hope that helps, DMG |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.