|
In the Bundle extension, the fatalError kills the app when trying to decipher the Astronaut.json text. I checked the text coming in and that looks ok. I even made a """ """ string with one astronaut and used the previous lesson's json decoder and it also fails to parse. I thought it might be a bad character but I eventually found it to be the \" and \n literals in the text which are causing the parsing to fail. I tried a clean build and both xcode 13.1 and 13.2 b2 have the same problem. The decoding works on all the examples I tried which don't include these escape characters. Anyone else run across this? guard let loaded = try? JSONDecoder().decode([String: Astronaut].self, from: data) else { fatalError("Failed to decode (file) from bundle.") } |
|
In this post from Paul there is a proper error handling for JSON. We only know that the the JSON can't be decoded because the app crashes when decoding is failing. Replace your guard statement with a proper do catch block and use the code Paul provided in the linked post. If you find out what the problem with the JSON is you can post it here or post the error message so we can help you. PS: Paul's tutorial code usually works. I don't want to sound to be rude. But with a proper error handling we can spot the problem which could be a specific to your environment. |
|
Thank you for the response and help. The problem went away after I finder copied the "astronauts.json" file and used the copy in the project (bundle). I then tried the original file I had downloaded from Github and it also worked. Although the code is working now, I am not happy about it becuase I don't know why it was not working before. I apologize if I sounded like the tutorial code was bad. That was not my intent and I assumed this code had already worked for most/all people in the past and I had some strange configuration/environment issue maybe (if I was lucky) one other person had. I also found Paul's post where he provides the more detailed json decode routine. This also worked fine without finding an error. It is good to have this routine in the toolbox. Again, thank you for taking the time to help. |
|
@ROWard, I think your response is exactly why the multi step validation is critical for new programmers to know.
There are so many steps where just reading the data can go wrong! It's too bad you didn't figure out why you had the initial problem. Perhaps you introduced a stray character in your JSON ? Who knows, you've moved on. But the next time, you'll have better clues. Please mark @hatsushira's response as SOLVED. |
|
Hi JSON files have a specific format and if you miss a comma or bracket from the file then it will not be able to decode it. I was going to suggest that you use a JSON decoder programme there are some online however I use Ducky Model Editor and would of pasted the JSON in it and it would of tell you where the error in the JSON code was. I also going to suggest is to downlaod the files again. |
|
In this case the video show a shortened version of the code below which This code is great and has the option to change the
If you want to use then you need to change the call to decode the date correctly as it a customize date.
|
|
@Hatsushira I've got exactly the same error as @ROWard47 has by following the Day 40 lesson. After I replaced all instances of However, I'm on the Day 41 lesson, as for some reason the
I'll try to debug with the |
|
@ROWard47 It seems that I was using the wrong version of the Here's how my initial version of the file was looking:
The Xcode yelled at me that I'm trying to compile an array instead of a dictionary. And here's how the updated Paul's JSOn file looks like in the video:
It's a dictionary, not an array as it used to be. After reverting my code back to Day 40 version, commenting out new stuff like Maybe this will help somebody who will stuck with the same issue :) Cheers! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your 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.