|
I'm working on my first Swift app, and I've run into an issue that I'm not sure how to debug. I've decided to use JSON files to store the data for my app, and I have one I use for previews. In this case, it's a list with multiple items. The file is loaded using a Bundle extension similar to what was shown in the Moonshot example couse app (with a few additional detailed error messages if something goes wrong). The source data for the list in question (from the JSON file) is this:
The issue I'm having is that on my list detail view, the items aren't showing. There are no errors, and the correct title for that list shows (which comes from the file), but no items. This is the case whether the preview loads "directly" from this view file, or if I navigate to it from the parent (which has a list of lists) -- the items won't show. Here's the code:
EDIT to clarify: The above code displays a screen with a title ("Preview List") and nothing else -- blank. The view code is really simple, so I think there must be a problem with how the data is being loaded from the file, but I'm not sure how to debug that -- how do I "see"/inspect an instance of an object? Here's the model for WishList:
What am I missing? |
|
If the problem is that nothing is showing in your list detail view, please show some code for the list detail view. Although I just noticed that the sample JSON you provided above consists of a single
|
|
I did include the WishList detail code, that's what I posted as And yes, I decode multiple lists from a single file in the preview decode, and if you look just a couple lines down, you'll see I'm returning a NavigationView that contains a single WishList. Hence why I'm able to display the title of that wish list just fine. |
|
What I think
Lets tidy it all up a bit
Add a file called
Change the json file
The Model
Now the
|
|
@NigelGee Thanks for trying to help, but I already have it setup that way.
No, I'm not. The JSON has an array of lists in it. I only showed the relevant one in my first post in an attempt to keep the post from getting too long. This is not the problem. I already use that Bundle-Decodable helper, and I already have an array of Wishlists in my file. Removing the brackets from the wishlist load prevents the project from compiling at all, because my JSON is already an array of list objects. And remember, the title of the list is displaying fine. If it wasn't able to load the data, the title wouldn't show. It's the items within the list that won't display. So for clarity, even though it's going to be really long, here is all the code. This is still exactly what I started with, I haven't made a single change from my initial post that's worked. Starting with my complete JSON file:
The complete WishList detail view:
The Bundle-Decodable extension I'm using (tinkered with a few different variations):
It all compiles/runs without error, but the preview shows this: |
|
Take the PS when
|
|
OMG. Thank you. That did it. facepalm I knew it was probably something simple I was overlooking. I'm just not very familar with SwiftUI behaviors yet. Much thanks. |
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.