【问题标题】:Xcode IOS5 retrieve filenamesXcode IOS5 检索文件名
【发布时间】:2012-04-27 20:34:34
【问题描述】:

我希望从根目录中检索所有扩展名为 *.gs 的文件名并将它们存储在一个数组中。

我尝试使用 directoryContentsAtPath.. 但它说这个方法在 ios5 中已被弃用。你知道任何替代方案吗?

【问题讨论】:

    标签: objective-c xcode ios5


    【解决方案1】:

    你应该使用NSFileManager的:

    – contentsOfDirectoryAtPath:error:
    

    (见Apple's documentation on NSFileManager。)

    你最终会得到类似的东西:

    NSString *path = @"your/path";
    NSError *error = nil;
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    

    如果您觉得不需要检查潜在错误,您可以将nil 传递给error 参数。但是,我建议您检查是否发生错误并在这种情况下显示适当的错误消息。你可以这样做:

    if (error) {
        // display some error message here
    } else {
        // process filenames returned by NSFileManager
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-30
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多