Cthutu
Cthutu

Reputation: 8917

Alternative way of finding documents directory on iOS

Usually the answer to finding the documents directory on iOS involves code like this:

+ (NSString *) applicationDocumentsDirectory 
{    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

but, I have discovered an alternative version:

+ (NSString *)applicationDocumentsDirectory
{
    return [@"~/Documents" stringByExpandingTildeInPath];
}

Is there any reason why I can't use this? Perhaps the Documents directory may not be in the home directory in the future?

Upvotes: 2

Views: 196

Answers (2)

Shaggy Frog
Shaggy Frog

Reputation: 27601

Is there any reason why I can't use this? Perhaps the Documents directory may not be in the home directory in the future?

I think you answered your own question here.

The first method asks the OS where the Documents directory is and assumes nothing.

The second method assumes the location of the Documents directory.

Upvotes: 3

What about this

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

Upvotes: -1

Related Questions