views:

3627

answers:

2

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(NSLibraryDirectory, NSUserDomainMask, YES);

Are there any particular reasons to use one over the other?

+18  A: 

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) is the suggested method.

Ben Gottlieb
Cool, that's what I'll use, but why it and not the other way?
bwinton
You can use the other way, but if an upgrade to the OS changes the default structure, this answer is guaranteed to still work with the new layout while the first way will either fail or start re-creating legacy directories.
Jason Coco
Why do you use NSLibraryDirectory rather than NSDocumentDirectory?
Elliot
Oops, copied the wrong bit of code. Fixed, thanks!
Ben Gottlieb
+7  A: 

Here is the code that I use in my framework.

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
Lee