I'm reading Erica Sadun's iPhone Developer's Cookbook, and ran into a question.
She says in the book that the way to find the user's Documents directory is with the code:
[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
but that seems slightly brittle, and dissimiliar to the normal Mac way of doing it, which would be:
NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
Are there any particular reasons to use one over the other?
Every iOS app gets a slice of storage just for itself, meaning that you can read and write your app's files there without worrying about colliding with other apps. This is called the user's documents directory, and it's exposed both in code (as you'll see in a moment) and also through iTunes file sharing.
You can do a Control-F search on an iPhone's browser by using the "On This Page," "Find in Page," or Share features. Control-F is a computer shortcut that locates specific words or phrases on a webpage or document. You can search for specific words or phrases in Safari, Google Chrome, and Messages.
Touch and hold the file or folder, then choose an option: Copy, Duplicate, Move, Delete, Rename, or Compress. To modify multiple files or folders at the same time, tap Select, tap your selections, then tap an option at the bottom of the screen.
Objc:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
Swift:
var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
You'll want the first element of the returned array.
Here is the code that I use in my framework.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With