|
Background I completed the iDine tutorial here on hackingwithswift.com. 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 JSON is the menu. 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) - MenuSection and MenuItem are the structs inside the app to decode the JSON. I'm trying to improve my understanding of coding JSON with SwiftUI so I am trying to add a third layer to the menu. My menu is for a pizza place with three sizes of pizza. For my instance, the first layer (MenuSection) would be pizza. The second layer (ItemType) would be cheese pizza. The third layer (MenuItem) would be a large, med, or small cheese pizza. Problem When I try to run the app, the build succeeds but the app then crashes because it cannot decode the JSON. The way the JSON gets decoded is inside of a file called Helper.swift that can be seen below or in the source code. I tried using the debugging statements from this HWS Forum post to see what is going on but the way that the JSON is being decoded in Helper.swift somehow prevents me from building the code - I'm unsure why (the error is that no errors occur in the do statement; however there certainly are errors occuring when there is no do - hence my post). I'm struggling to get more information on what is failing when trying to decode the JSON. I have already used JSONlint.com to make sure that my JSON is formatted correctly. I've checked that the names of the attributes inside my structs have the same names as the JSON values (no need to use a decoding scheme). I've checked to make sure all JSON values have a corresponding attribute inside of a struct to map to. I have three unique structs so that the three levels of my JSON can be decoded. Code Here is my code for reference. First, the JSON (menu.json):
Next, here are my structs (Menu.swift):
Lastly, here is where the JSON initially gets decoded (Helper.swift):
Note: the error that I recieve when trying to run the app is Any help is appreciated. Thanks! |
|
Number one advice when having trouble decoding JSON: DO NOT use (Once development is done and you know your code works and your JSON is valid, then you can change back to using Do something like this instead:
If you do, you will see an error like this:
This error is because most of the UUIDs you have in your JSON are not valid UUIDs.
(In case it's not obvious, the hyphens |
|
@roosterboy thank you! Incredible. For some reason the do/catch statement that I tried to use from this comment wasn't working but I think it's because I had And thank you for the info on UUID. As you can tell, I'm a novice. After reading the wiki on UUID, it all makes sense as to why the app was getting hung up on decoding those |
|
Yup. In fact, I'm a little surprised Xcode didn't give you a warning that the |
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.