Question

For my program, I need to make a directory on the desktop (eventually I'll change that, but its easier to do it there for now) on a OS X. From the examples I've read, this should work, but it doesn't. I don't get any errors from Xcode, and neither do I get any errors made by my program using the alternate attempt on the very bottom. It also doesn't make a directory called ProjAlleleData on my Desktop either.

NSString *dirName1 = @"~'Desktop/ProjAlleleData";
NSFileManager *fm;
fm = [NSFileManager defaultManager];    
numb1 = 1;
//Makes sure directory exists and has correct permissions.
while ( numb1 == 1) {
    if ([fm fileExistsAtPath: dirName1] == YES) {
        if ([fm isWritableFileAtPath: dirName1] == YES) {
            if ([fm isReadableFileAtPath: dirName1] == YES) {
                numb1 = 0;
            }
            else {
                NSLog(@"Permissions error.");
                return 1;}
        }
        else {
            NSLog(@"Permissions error.");
            return 1;}
    }
    else {
        //create directory
        [fm createDirectoryAtPath: dirName1 withIntermediateDirectories: YES attributes: nil error: NULL];
    }
}

Alternatively (and preferably as it will be easier to debug if need be), below the //create directory:

        if ([fm createDirectoryAtPath: dirName1 withIntermediateDirectories: YES attributes: nil error: NULL] != YES) {
            NSLog(@"Save File Creation Error. (dir1)");
            return 1;
Was it helpful?

Solution

You have two errors in your first line. First change

NSString *dirName1 = @"~'Desktop/ProjAlleleData";

to

NSString *dirName1 = @"~/Desktop/ProjAlleleData";

then you have to expand the ~:

NSString *dirName1 = [@"~/Desktop/ProjAlleleData" stringByExpandingTildeInPath];

That should give you a string like /Users/yourname/Desktop/ProjAlleleData

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top