Saving Data in iOS - Part 2: Section 1: Document | Ray Wenderlich

The user's document folder is a great place to store data. Where is it located? The File Manager knows!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4307-saving-data-in-ios/lessons/2

Learning a lot from you guys! Kudos for the Swift 4 stuff! amazing!
Also the playground usage :sparkles:

Now for my questions:
1.)
The document directory for the user is not clear. In a sandbox the document directory is the apps folder and the user is the app, right?

2.)
Why not use NSHomeDirectory():

let url = URL(fileURLWithPath: NSHomeDirectory())

are there cons for choosing the home directory? it seems like the root folder of the documents folder.

Thanks :slight_smile:

Glad to hear that! Here’s the way I see all this:

  1. As you can tell from the NS prefix, and the fact that it is a String, not a URL, NSHomeDirectory represents a way of doing things not only pre-Swift, but pre-iOS. Appending "/Documents" to NSHomeDirectory has so far always resulted in the path of the user’s document directory, but it may not always, even on macOS. Take one of the approaches FileManager offers: it’s more future-proof, and is intended to work on platforms outside of Apple’s, as well.

  2. Perhaps 2’s answer clears up 1 as well? You shouldn’t rely on the document directory being named “Documents”, and you shouldn’t rely on it actually being a subfolder of any other folder in the sandbox – that includes the root directory. Using Foundation’s APIs, you don’t need to. You work with an abstraction of added-together paths, in the form of URLs.

Great job on the Saving Data in iOS series. I have a question regarding the API for retrieving URL’s. It makes sense to me why would use this versus just typing in a string that could change. However, I’m not sure I understand why we have to reference the first index in the URL’s that are returned when using urls(for:, in: )?

1 Like

I really like the course, particularly your light and bouncy delivery.

Nevertheless I had to watch it several times because I feel you didn’t set the scene by clearly telling me where my files end up. I’m still confused about the directory structure since all our work ended up on my computer and not on a device. Is it different there?

A tree-graph would be really helpful. Can I poke about on my device and see my files?

You mention it is the “system’s” responsibility for creating the directory and so I experimented with different values in the two OptionSet parameters and none of the others created a directory. So what are the rules?

Thanks

1 Like

I’m still confused about the directory structure since all our work ended up on my computer and not on a device. Is it different there?

A tree-graph would be really helpful. Can I poke about on my device and see my files?

You can! You’ll need to alter your Info.plist, however.

You mention it is the “system’s” responsibility for creating the directory and so I experimented with different values in the two OptionSet parameters and none of the others created a directory. So what are the rules?

Apple’s documentation has good information on what’s available. I agree with you that the FileManager API documentation doesn’t do much to help us out!

Thank you Jessy. That’s a terrific solution. It beats me how you guys stay on top of all this stuff.

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.