Previous episode: 01. Introduction
Next episode: 03. Paths
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 wanna save data in a file. But where do files go on iOS? The file manager class can help you out with that. It's job is to help you interact with the file system and it's contents. This is what this video is all about. Due to sandboxing restrictions on iOS, only a handful of directories are available in which to store and manage your files. The users document directory is a common, and appropriate place to store data. You can access this directory or folder directly from playgrounds. Which you'll use in the first part of this course. iOS apps are isolated from one another. And the way this is achieved, is by giving each app its own sandbox. A place where you can store and manipulate files exclusively within your app. In Swift, directories are represented using the URL struct. You'll be using file manager to get the document directory URL. Open a new blank playground and delete any code it starts with. Everything you'll be working with for now is in the foundation framework, so start by importing that. Then access the default file manager. You can make other file manager instances, but for most of your tasks, using the default file manager will work just fine as all of its methods are thread safe. If thread safe doesn't mean anything to you yet, that's okay. Just let FileManager.default do the heavy lifting for you. One way to find the users document directory, is to use file manager's instance method named URLs. The first argument is where you dictate that you want a document directory. The second argument, user domain mask, is how you tell the file manager that the directory belongs to the user. The URL's method, as its name suggests, returns an array of URLs. But there's only one document directory per iOS app. If you want that single URL, subscripting at the array's only valid index, zero, will grab it for you. The URLs method is very general purpose, which makes it very verbose and lengthy to access the document directory. Considering how much you'll be using this directory, you're going to refactor your code to make the process easier. How bout adding a computed property called, document directory URL? This is much better, however if you're actually to use this short cut across apps, it might be more appropriate to put it inside of a file manager extension. Since you don't need a specific instance of file manager to access this, go ahead and make the property static. Because default is also a static property of file manager, you don't need to explicitly use file manager in your computed property anymore. But, by not using it, you need to add backtics because default is a Swift keyword. Then you can access your newly created property like so. A good place for reusable code in playgrounds is the sources folder. Hit Command + 1, and click on the arrow next to the name of the playground if it's not already expanded. When sources is selected, you can hit option Command + N, to make a new folder. Extensions is a good name for what you need. Then you can hit Command + N, without option, to make a new file. Call this new file, file manager. If your new file is already inside the extensions folder, fantastic, if not, you can drag the file into the folder. Notice that this new file already has foundation imported, as you need. Go back to the playground, and cut the code you wrote, by making a selection and hitting Command + X. Then, just paste it in the new file manager file with Command + V. Go back to the playground once more, and try accessing the property in your extension. Xcode's auto complete didn't work, and there's an error in the playground's debug area. It's telling you that the protection level of the computed property is internal. And it's telling you this in the debug area. The error in the main editor simply says that it has no member document directory URL. To get it to work outside of the filemanager.swift file, you need to make the extension public. Now your playground should be compiling as expected. As you work with file manager and directories, you will need to see the structure and contents of your directories and data. An easy way to access the document directory, is to right-click or option click the file://URL that is shown on the right side of your playground, and then select, open URL. This is the document directory for your current playground, neat.
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.