"There is no such file or directory" Error creating directory

I am trying to save thumbnails of user-created photos in a subdirectory in the document directory, but I cannot create a subdirectory in the first place. When I try to create a subdirectory in the document directory, I get this error (there is no such file or directory):

Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)"
UserInfo=0x17ef9c90 {NSFilePath=/var/mobile/Applications/FD0CC480-97FE-4E50-931F-5341DB6AD92B/Documents/com.Jonathan.App-Name/707F0431-7A09-4D33-B122-13C48AD4CA53, 
NSUnderlyingError=0x17e51520 "The operation couldn’t be completed. No such file or directory"}

I'm so confused. I know that the directory does not exist, because I'm trying to create it! The only thing I can think of is that the document catalog is incorrect, but it is straight from Apple Docs.

Am I trying to create a subdirectory incorrectly?

- (NSURL *)documentsDirectory
{
    NSFileManager *sharedFM = [NSFileManager defaultManager];

    NSArray *possibleURLs = [sharedFM URLsForDirectory:NSDocumentDirectory
                                             inDomains:NSUserDomainMask];

    NSURL *appSupportDir = nil;
    NSURL *appDirectory = nil;

    if ([possibleURLs count] >= 1)
    {
        // Use the first directory (if multiple are returned)
        appSupportDir = [possibleURLs objectAtIndex:0];
    }

    // If a valid app support directory exists, add the
    // app bundle ID to it to specify the final directory.
    if (appSupportDir)
    {
        NSString *appBundleID = [[NSBundle mainBundle] bundleIdentifier];

        appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
    }

    return appDirectory;
}

- (NSURL *)thumbnailDirectoryWithName:(NSString *)theName
{
    NSURL *directory = [[self documentsDirectory] URLByAppendingPathComponent:theName];
    NSURL *thumbnailsDirectory = [directory URLByAppendingPathComponent:@"Thumbnails"];

    NSError *error;

    if ([[NSFileManager defaultManager] fileExistsAtPath:[thumbnailsDirectory path]] == NO)
    {
        [[NSFileManager defaultManager] createDirectoryAtURL:thumbnailsDirectory withIntermediateDirectories:NO attributes:nil error:&error];

        if (error)
        {
            NSLog(@"[Thumbnail Directory] %@", [error description]);

            return nil;
        }
    }

    return thumbnailsDirectory;
}

I also tried createDirectoryAtPath:and to stringByAppendingPathComponent:no avail.

NSURL *directoryin thumbnailDirectoryWithName:returns:

/var/mobile/Applications/FD0CC480-97FE-4E50-931F-5341DB6AD92B/Documents/com.Jonathan.App-Name/707F0431-7A09-4D33-B122-13C48AD4CA53

, 707F0431-7A09-4D33-B122-13C48AD4CA53, Entity in Core Data, .

!

+4
2

:

- (NSString *)documentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return (paths.count)? paths[0] : nil;
}

, :

- (NSURL *)thumbnailDirectoryWithName:(NSString *)theName {

    NSString *namePath = [[self documentsDirectory] stringByAppendingPathComponent:theName];
    NSString *thumbnailsPath = [namePath stringByAppendingPathComponent:@"Thumbnails"];
    NSURL *thumbnailsURL = [NSURL fileURLWithPath:thumbnailsPath];

    NSError *error;

    if ([[NSFileManager defaultManager] fileExistsAtPath:thumbnailsPath] == NO) {
        [[NSFileManager defaultManager] createDirectoryAtURL:thumbnailsURL withIntermediateDirectories:NO attributes:nil error:&error];

        if (error) {
            NSLog(@"[Thumbnail Directory] %@", [error description]);
            return nil;
        }
    }
    return thumbnailsURL;
}
+5

@danh, . , , , -. , Apple , , , withIntermediateDirectories:YES.

withIntermediateDirectories:

, URL-. , , .

, , .

+11

All Articles