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.
You still need to correctly set up the initial power test tasks array so your app doesn't crash if you were to run it on a different simulator or on a device. There's also a cool trick I wanna show you of something that's been happening automatically for you behind the scenes. And what to do if you want to use different variable names and JSON keys when encoding or decoding data. Let's start with the array fix. The current issue that results in a crasher unless you copy the initial prioritized tasks file is that you are initializing prioritized tasks as an empty array, when it needs to be an array of one prioritize tasks per priority. At the following instead of the empty prioritize tasks array, initial value. Run your app on a device or on a different simulator now, if you want you can erase the contents of your current simulator, to make sure this fix is working. Already you can tell things are different as you have empty putty sections as where as before, without the initial file, you had a blank screen with the buttons at the top. Another thing to note is that, because we no longer have the prioritizetasks.JSONfile, Whenever you're loading the file, upon launching the app, it is throwing an error. This is why we wrapped the call to load the file, in a do-catch statement. To prevent the application from crashing, the first time that you launch the app, when no file has been stored with any tasks at all. Add a new task and the crash is now gone. If you stop and re-run the app, your task should also be there too. Fantastic! Now, what happens when you want to encode or decode a type that conforms to codable, which also has a custom type as a property. Guess what? You've already been doing this. Open taskstore.prioritizetasks.swift and notice how your structure at its core, has a property for your custom type priority, and an array of your custom tasks. The rule to making this nested setup work, is to ensure that all your properties within the type, also conform to codable. Fortunately, Xcode is pretty good at letting you know when they don't and will throw an error even before you can run your app. Now, what happens if you want to take your task struct and store the ID or completed properties under a different key in your JSON file? This is where coding keys come into play. Open task.swift and within the structure definition have the following enumeration: This is an enumeration of strings that conforms to the coding key protocol. What this protocol does is let you specify what your property names encode or decode into from your JSON file. When you add this enumeration, you need to specify cases for each of the properties you want to encode. So you want to at least add one case. Because up until now you were using the property names as the keys, You didn't need to do this. Now that you want to customize them in the JSON file, then you have to add them to the enumeration. At some cases, you can tweak the keys used in the JSON file. With this, you are telling codable that you want to use name as the key for storing your task name. But for the ID you want it to be stored under the identifier key, and for completed, you want to store it in the JSON file as is complete. Run your app and if you wanna see the resulting JSON file, use the simulator so you can see the prototypetasks.JSONfile. Add a task, then check out the resulting file. Your JSON file has keys for a task that differ from the property names you used in your app. If you have any trouble running the app, we're loading it from the previous session, simply delete the prioritizetasks.JSONfile or restart your simulator. Now what we've done will come in handy when you're loading data from a web service that perhaps doesn't have the nicest key names or they're different than what you have in your properties. And you don't want to be forced to use those as your property names.
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.