“没有这样的文件或目录”创建目录时出错

时间:2013-12-16 02:54:30

标签: ios nsfilemanager

我正在尝试将用户生成的照片的缩略图保存在文档目录的子目录中,但我无法在第一时间创建子目录。当我尝试在文档目录中创建子目录时,我收到此错误(没有这样的文件或目录):

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"}

我很困惑。我知道目录不存在,因为我正在尝试创建它!我唯一能想到的是文档目录不正确,但这直接来自Apple Docs。

我是否错误地尝试创建子目录?

- (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's 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;
}

我也试过了createDirectoryAtPath:stringByAppendingPathComponent:但没有用。

NSURL *directory中的thumbnailDirectoryWithName:返回:

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

文件路径的最后一个组件707F0431-7A09-4D33-B122-13C48AD4CA53是核心数据中实体的唯一标识符,它使我能够唯一地命名目录。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:15)

我接受了@danh的答案,但我找到了收到此错误的原因。这是一个简单的错误,也是我厌倦时不应该编码的另一个原因,哈哈。我找不到Apple建议我们将应用程序包标识符作为子目录名称包含的原因,但为了解决此错误,我只需要设置withIntermediateDirectories:YES

<强> withIntermediateDirectories:

  

如果为YES,则此方法将创建任何不存在的父目录   在url中创建目录的一部分。如果否,则此方法失败(如果有)   中间父目录的名称不存在。

基本上,我试图在目录中创建一个不存在的子目录。

答案 1 :(得分:6)

以下是获取文档目录的方法:

- (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;
}