如何检查Objective-C中是否存在目录

时间:2011-09-24 21:08:49

标签: iphone objective-c cocoa-touch nsfilemanager

我想这是初学者的问题,但我试图检查iPhone上的Documents文件夹中是否存在目录。我阅读了文档并提出了这段代码,遗憾的是,它在BOOL fileExists行中与EXC_BAD_ACCESS崩溃:

 -(void)checkIfDirectoryAlreadyExists:(NSString *)name
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSString *path = [[self documentsDirectory] stringByAppendingPathComponent:name];

    BOOL fileExists = [fileManager fileExistsAtPath:path isDirectory:YES];

    if (fileExists)
    {
        NSLog(@"Folder already exists...");
    }

}

我不明白我做错了什么?它看起来对我来说都很完美,它肯定符合文档,不是吗?关于我哪里出错的任何启示都将受到高度赞赏!感谢。

更新:

仍然没有工作......

  -(void)checkIfDirectoryAlreadyExists:(NSString *)name
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSString *path = [[self documentsDirectory] stringByAppendingPathComponent:name];

    BOOL isDir;
    BOOL fileExists = [fileManager fileExistsAtPath:path isDirectory:&isDir];

    if (fileExists)
    {


        if (isDir) {

            NSLog(@"Folder already exists...");

        }

    }

}

3 个答案:

答案 0 :(得分:85)

查看documentation此方法签名:

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

您需要一个指向BOOL var作为参数的指针,而不是BOOL本身。 NSFileManager将记录该文件是否是该变量中的目录。例如:

BOOL isDir;
BOOL exists = [fm fileExistsAtPath:path isDirectory:&isDir];
if (exists) {
    /* file exists */
    if (isDir) {
        /* file is a directory */
    }
 }

答案 1 :(得分:10)

以防万一有人需要一个getter,如果它不存在,会在Documents中创建一个文件夹:

- (NSString *)folderPath
{
    if (! _folderPath) {
        NSString *folderName = @"YourFolderName";
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [documentPaths objectAtIndex:0];
        _folderPath = [documentsDirectoryPath stringByAppendingPathComponent:folderName];

        // if folder doesn't exist, create it
        NSError *error = nil;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDir;
        if (! [fileManager fileExistsAtPath:_folderPath isDirectory:&isDir]) {
            BOOL success = [fileManager createDirectoryAtPath:_folderPath withIntermediateDirectories:NO attributes:nil error:&error];
            if (!success || error) {
                NSLog(@"Error: %@", [error localizedDescription]);
            }
            NSAssert(success, @"Failed to create folder at path:%@", _folderPath);
        }
    }

    return _folderPath;
}

答案 2 :(得分:2)

我有一个Utility单例类,我用它来做这样的事情。由于我无法更新我的数据库,如果它仍然在Documents中,我使用此代码将我的.sqlite文件从Documents复制到/ Library / Private Documents。第一种方法找到库。第二个创建私有文档文件夹(如果它不存在)并将该位置作为字符串返回。第二种方法使用@wzboson使用的相同文件管理器方法。

+ (NSString *)applicationLibraryDirectory {

            return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
}


+ (NSString *)applicationLibraryPrivateDocumentsDirectory {

    NSError *error;
    NSString *PrivateDocumentsDirectory = [[self applicationLibraryDirectory] stringByAppendingPathComponent:@"Private Documents"];

    BOOL isDir;
    if (! [[NSFileManager defaultManager] fileExistsAtPath:PrivateDocumentsDirectory isDirectory:&isDir]) {

        if (![[NSFileManager defaultManager] createDirectoryAtPath:PrivateDocumentsDirectory
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error]) {
            NSLog(@"Create directory error: %@", error);
        }
    }

    return PrivateDocumentsDirectory;
}

我在持久存储协调器初始化中使用它。同样的原则适用于任何文件。

NSString *libraryDirectory = [Utilities applicationLibraryPrivateDocumentsDirectory];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:sqliteName];
NSString *destinationPath = [libraryDirectory stringByAppendingPathComponent:sqliteName];