Alex
Alex

Reputation: 11157

why can't i create a folder using this code?

I am trying to create a folder in my /Documents named /coffeeShops.I have the following code in my application:didFinishLaunchingWithOptions

 NSArray *dirPaths;
    NSString *docsDir;

    NSFileManager *manager;
    NSError *error;

manager =[NSFileManager defaultManager];


dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                               NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];
NSString *coffeeString=[NSString stringWithFormat:@"@/@",docsDir,@"coffeeShops"];

if (![manager fileExistsAtPath:coffeeString isDirectory:YES]) 
{
    [manager createDirectoryAtPath:coffeeString withIntermediateDirectories:YES attributes:nil error:&error];


    NSLog(@"Creating folder");
    if(error) NSLog(error);
}
else NSLog(@"Folder for coffee pics exists");

i get an EXC_BAD_ACCESS at

if (![manager fileExistsAtPath:coffeeString isDirectory:YES]) 

what am i doing wrong ?

Upvotes: 0

Views: 111

Answers (1)

deanWombourne
deanWombourne

Reputation: 38485

isDirectory is designed to tell you if the specified file is a directory or not, not for you to say 'only directories please'.

Try this instead :

BOOL isDirectory = NO;
BOOL exists = [manager fileExistsAtPath:coffeeString isDirectory:&isdirectory];
if (NO == exists || NO == isDirectory) {

@Evan did have an answer that had some more useful points in but it's vanished :(

There's a method on NSString that builds paths for you - it's called stringByAppendingPathComponent. I think that you should be using it like this :

NSString *coffeeString=[docsDir stringByAppendingPathComponent:@"coffeeShops"];

Upvotes: 3

Related Questions