|
I'm working on project Milestone: Projects 13-15 (Hot Prospect) and have working code with the exception of saving the array (prospects: [Prospect]) that holds all the prospect data. Because the Prospect class includes an image (that comes from an ImagePicker) I had to conform the class to Codable by hand in order to be able to JSONE encode/decode the data for saving and loading. Code below. Any insights would be much appreciated. Prospect Class
ContentView
|
|
"Array Not Saving"... What exactly does this mean? Do you get an error? A silent fail? Does the rest of your JSON save and just the image doesn't? etc. But the first thing you should do is replace your meaningless error messages with something that gives you an idea what happened when something goes wrong:
What do you get after you do that? |
|
Thanks for your help. What I mean by "Array not saving" is that when I enter some prospects via the PhotoNameView those entries are not being loaded when I quit the app and start it again so nothing is being saved. Per the suggestions... I added the print(error) calls as suggested and no errors are printing. Also, I added print calls to print the prospect array at app initializaton and after the save() function is called (just to be sure it is being called) as well as one to print the save path. Here is what I get in the console... Content of prospects at app startup: [ ] <---empty prospects array at app startup even after Save complete <---save function is called. file:///Users/markmeihaus/Library/Developer/CoreSimulator/Devices/199EE113-5C58-4A56-AC98-F17B2EB23F7C/data/Containers/Data/Application/B1ECD931-0612-4FFA-8B24-95BA0B1F10E6/Documents/SavedProspects Content of prospects after calling save(): [HotPropsects.Prospect] After reviewing my code again I think perhaps the issue is when I initialize the PhotoNameView (see arrow below). The prospects array is an ObservedObject so it needs initialization. I think the way I have it written is that every time the PhotoNameView is called I initialize the prospect array with an empty Prospect object? But even so I should be saving at least one entry. Could that be the issue? Any thoughts on how to initialize the PhotoNameView if my current code is incorrect?
|
|
Hi,
Try changing the
|
|
Brilliant! That did it. Thanks a bunch @Hectorcrdna. Now I need to figure out why that works and not how I originally coded. One additional question if you don't mind...How do I add images to the picker view in Xcode? Right now when I call the picker I get some stock photos but I would like to add some other ones more appropriate for the project. Any thoughts? |
|
As far as why i know it has to do with it being an @State property, you could also go into the property value like this
but i think i read it could have side effects, not entirely sure; and to add images you just drag and drop them in the simulator and they save to the images app. |
|
Got it, thanks. One final question if you are so inclined...I've listened to/read/reviewed Paul's discussions on property wrappers and using _Object rather than just Object when assigning values but I'm still trying to get my head around it. When usingState or ObservedObject functions there is another parameter called wrappedValue. How would that differ from using the initial value one? |
|
I quess it would not differ, the documentation for |
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.