【问题标题】:How to check if folder is empty, and instantiate file names inside the folder into NSStrings? (iphone cocoa)如何检查文件夹是否为空,并将文件夹内的文件名实例化为 NSStrings? (iphone可可)
【发布时间】:2011-11-12 14:24:49
【问题描述】:

只是想知道,我将如何检查特定文件夹是否保存文件,并将文件夹内的文件名实例化为 NSStrings?我知道一个名为 NSFileManager 的类,但我不确定如何应用它来满足我的目标。

【问题讨论】:

    标签: iphone objective-c nsfilemanager


    【解决方案1】:

    默认情况下,您的所有自定义文件和数据都将存储在应用程序的文档目录中。我在下面放了一个示例代码来访问默认的文档目录;加上您可能在其中拥有的一个名为“MyFolderName”的自定义文件夹

    最终结果将是一个数组,其中包含您指定路径中文件或目录的 NSString 对象列表。

    //Accessing the default documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    //Appending the name of your custom folder, if you have any
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MyFolderName"]; 
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:path]) { // Directory exists
    NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:path error:nil];
    }
    

    希望这会有所帮助! :)

    【讨论】:

    • 没想到会这么简单。谢谢!
    【解决方案2】:
    NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderLocationString error:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2010-10-19
      • 1970-01-01
      相关资源
      最近更新 更多