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.
In this video, you'll begin working with files in the users app sandbox. Up until now, you were decoding the JSON data from files in your app bundle. That is, in files that were already added to your application prior to building your application binary. Now you will start saving and manipulating files to the sandbox, where a few folders are available to you. You'll continue to work in the loadJSONPrioritizedTasks method, and start by adding print statements to visualize the path of your app sandbox versus your app bundle. Add a print statement at the top of the loadJSONPrioritizedTasks method. Then, run the app in the iOS simulator and look at the console. You can copy the URL's path, click on your desktop or the finder icon, and then hit command shift G. Paste the path in there. Alternatively, you can also go in finder to the go menu option and go to folder, which is the same as the shortcut you just entered. Of note is that when copying the URL, you're not copying the portion that says file://, but rather just the path that starts at /Users. That is because the forward slash indicates it's the root folder of your drive, which, in this case, contains a folder named Users, and then your username and everything else, which will take you to your application bundle. I'm going to paste this now and hit the return key. Note that even though your path may have included the /TaskList.app in the path, finder takes you one folder up. What you can do to truly go into the app bundle is right click the TaskList app and then select show package contents. This is your app bundle, and as you can see, it contains your resources, including the JSON files being loaded. The bundle, in this case, corresponds to your iOS app and all the contents that get added when creating it via xcode. Next up, add a print statement to show your users document directory URL. This will be the app-specific document directory. Run your app in the simulator still and follow the same process of, via the finder, hitting command shift G, or going to go, go to folder, to be able to enter the folder you wanna go to. This time, paste the path to the document directory. This corresponds to your app sandbox document directory. Right now, it's empty because you're not storing anything there to read back. The document directory is outside of the app bundle in a separate location within iOS that keeps your user data on a per-app level compartmentalized. There's one more directory that I want to show you, so add the following code right after your last print statement. Run the app in the simulator one more time and check out the path in the console. You don't need to open this folder if you don't want to, but notice how the majority of the path is identical except for the last directory within the sandbox. It's a folder called TMP, as opposed to documents, which is the one you'll regularly work with. TMP is a unique folder for when you want to store temporary data, cache any files, images, et cetera, that you don't need to ensure they are permanently persisted. If you were to encode and save your tasks into the TMP folder, iOS can purge its contents at any time, causing you to lose that data. This is why, for storing their very important set of tasks, their priorities and completed status, you use the more permanent solution in the document directory. Until you save your tasks to the document directory, go ahead and copy the PrioritizedTasks.json file from your project folder into the document directory printed out in the console. Note that, for this, you'll need to use the iOS simulator so you can easily copy and paste files into these folders, something not to easy or straightforward if done on a physical iOS device. You're going to get rid of the guard statement in loadJSONPrioritizedTasks and instead load the URL in a way you're already familiar with. That should be similar to how you constructed URLs in playgrounds when experimenting with the file manager URL and paths. File manager also provides you with a handy method, contentsOfDirectoryAtPath, to acquire a string array of the contents of a particular directory, which will be perfect for knowing what files are available in your different sandbox directories. The call can throw, so you mark it as try. And since it can return an optional, you simply nil coalesce it to an empty array. The only parameter this method takes is a string with the path that to the directory you want to get the contents for, and you print out the results so the array of files can be seen in the console. Build and run your app in the iOS simulator and, if the JSON file was copied to the right location, it should be printed out in the console when run. And there it is. You're now loading your tasks from the file in the document directory and not from your app bundle. This will get you halfway there when it comes to loading and decoding your serialized prioritized task data. The other half is saving the data to the file, and persisting that data when you make edits and across app launches.
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.