|
Background As the title says, I'd like to make an expanding list view (like Paul does in the previous link) but with JSON instead of hardcoded data. In both his article and video on the subject, he says it's too messy with JSON - I'm not even sure if this is something that I should be pursuing or if it's just a waste of effort. I'm using the iDine tutorial as a template. This is the source code (this will download the original iDine project to your computer - this link is found on the previous link's page about halfway down). Basically, it is a resturant app, the menu is JSON. It is a nested JSON menu with 2 layers; contents of the first layer are the MenuSections (ex. breakfast) and the second layer are the MenuItems (ex. pancakes) - Here is what the original structs look like:
Following Paul's example I've eliminated
Problem When I try to run the app, the build succeeds but the app then crashes because it cannot decode the JSON. It returns an error If I'm interpreting this correctly, XCode is complaining that there is no value photoCredit in the top layer of my JSON (which used to be MenuSection but is now just another instance of MenuItem - JSON seen below). Am I wrong to assume that if XCode doesn't find a matching value for a particular instance of a struct inside the associated JSON that it will just ignore it? I thought it would ignore it because in several other cases I've just had missing image files that would associate with Code Here is a portion of the JSON menu for reference:
Any help is appreciated. Thanks! |
|
The problem is that you have collapsed two diffferent types from your JSON into a single Swift type. The outer type in your JSON just has the properties Just as you made |
|
@roosterboy, is it possible to implement this expanding list view with a single level of JSON? How do you suggest that you make one node the parent and another the child?
|
|
@davidzimmerjr Try adding this to your List in ContentView: .listStyle(SidebarListStyle()) Here is the original link: https://stackoverflow.com/questions/65110710/what-are-the-requirements-for-a-free-expandable-list-in-swiftui |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.