Previous episode: 11. JSON Decoding
Next episode: 13. Saving On Device
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
If you type is encodable, not only can you decode an instance of it in JSON, you can store an array of its instances in JSON. For this challenge, update your loadJSON method, in Contentview.swift to use the new Tasks.JSON and prioritizedTasks.JSON files in order to load a Task array and a prioritized Task array. Print out the results to the console in order to see the loaded arrays, have fun. (upbeat music) The first thing I did was to update the guard statement inside loadJSON to point to the new JSON files to use. Then I updated the data initializers to use the new constant names, as well as some of the constant names to reflect that multiple Tasks and prioritized tasks are being loaded. Finally, instead of decoding a type of Task or prioritizedTasks, I updated this to reflect that an array of each one is being loaded. Run your app with these changes and check out the console. There is now a lot more content in the console to reflect the arrays being loaded. With the challenge completed, how about you start, updating the TaskStore to use the prioritized Tasks from the JSON file and not hard code them in the PrioritizedTasks property. First, start by removing the onAppear method in the body of content view. Next, select all of your loadJSONs method code and cut it by hitting Command + x. Content view should now just look as it was, before adding your method for loading JSON data. Switch over to the TaskStore.swift file and paste your loadJSON method. Xcode is showing a bunch of errors about unresolved identifiers. This can be addressed by importing the Foundation framework. So go ahead and add that to the top of the file. Excellent, those errors should now be gone. Time to clean up the method a little bit. Start by renaming it to loadJSON prioritizedTasks to better reflect what you'll be loading or decoding. You only need a single URL to load your PrioritizedTasks. So go ahead and update the guard statement to reflect that. The decoder constant as well as the catch statement, can remain the same. but you can also clean things up a bit within the do block when decoding the JSON array. Note how when decoding, you store the array of PrioritizedTasks in the task stores PrioritizedTasks property. That property, however, has some hard coded data already. Clean that up, so it starts out as an empty array instead. Whereas before the compiler could infer the type. You now explicitly have to declare it as an array of PrioritizeTasks. Build and run the app. It's empty, why is that? Well, the loadJSON PrioritizedTasks method is not getting called anywhere. A great place to do this is in an initializer for TaskStore. Now run your app again and check out the results. Excellent. Your app no longer uses hard coded data in your code, but instead is loading it from a file, in your apps bundle.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.