【问题标题】:How can you determine if a file exists within the app bundle?如何确定应用程序包中是否存在文件?
【发布时间】:2011-01-02 17:12:28
【问题描述】:

抱歉,今天的第 2 个愚蠢问题。是否可以确定文件是否包含在 App Bundle 中?我可以访问文件没有问题,即,

NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];

但一开始不知道如何检查文件是否存在。

问候

戴夫

【问题讨论】:

  • 因为 [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"] 如果文件不存在会返回 NULL,所以我通常只检查 if (pathAndFileName != NULL) { //file存在}
  • 您也可以不使用代码。在另一个论坛上查看我的帖子:forums.macrumors.com/threads/…
  • @moonman239 我需要做的检查需要在代码中,因为我正在使用缓存的缩略图预加载应用程序,因此应用程序的初始运行速度很快。我有另一个线程,然后下载一个新的数据文件。在显示数据时,应用程序需要检查图像是否在包中(预加载的缓存图像)。如果不是,则从服务器检索图像并将其保存到磁盘缓存中。希望这是有道理的。
  • @MagicBulletDave 我还是不明白。缓存的图像是应用程序附带的,还是下载的?如果它随应用程序一起提供,那么理论上代码应该不需要检查图片是否存在 - 应用程序可以假设它是。
  • 这是一种通用算法。该应用程序带有一个 plist 形式的数据文件。每行数据都有一个缩略图。首次加载时,所有内容都在捆绑包中,因此可以直接从中显示。定期下载一个新的 plist(包含一些现有数据和一些新数据)。现有数据的图像将在捆绑包中,新数据的图像需要下载然后缓存到磁盘。所以事件链是:首先查看捆绑包,然后是磁盘缓存,最后如果仍然没有图像,则尝试从服务器下载它。有意义吗?

标签: iphone nsbundle


【解决方案1】:
[[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName];

【讨论】:

  • 噢!谢谢 Rob,一直在使用它来存储文档目录中的文件!天色已晚。再次感谢。
  • 然而,根据this answer,即使是Apple 也建议实际上“尝试操作(例如加载文件或创建目录),检查错误,并优雅地处理任何错误而不是提前弄清楚操作是否会成功”.
【解决方案2】:
NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"filename"];
    if(![fileManager fileExistsAtPath:path])
    {
        // do something
    }

【讨论】:

  • 这是查看文档目录而不是应用程序包
【解决方案3】:

这段代码对我有用...

NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName])
{
    NSLog(@"File exists in BUNDLE");
}
else
{
    NSLog(@"File not found");
}

希望对某人有所帮助...

【讨论】:

    【解决方案4】:

    与@Arkady 相同,但使用 Swift 2.0:

    首先,在mainBundle() 上调用一个方法来帮助创建资源路径:

    guard let path = NSBundle.mainBundle().pathForResource("MyFile", ofType: "txt") else {
        NSLog("The path could not be created.")
        return
    }
    

    然后,在defaultManager()上调用一个方法来检查文件是否存在:

    if NSFileManager.defaultManager().fileExistsAtPath(path) {
        NSLog("The file exists!")
    } else {
        NSLog("Better luck next time...")
    }
    

    【讨论】:

      【解决方案5】:

      如果资源不存在,pathForResource 将返回 nil。用 NSFileManager 再次检查是多余的。

      Obj-C:

       if (![[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"plist"]) {                                              
            NSLog(@"The path could not be created.");
            return;
       }
      

      斯威夫特 4:

       guard Bundle.main.path(forResource: "FileName", ofType: "plist") != nil else {
            print("The path could not be created.")
            return
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-08
        • 1970-01-01
        • 2017-03-22
        • 2012-06-19
        • 1970-01-01
        • 1970-01-01
        • 2011-02-16
        • 2011-08-10
        相关资源
        最近更新 更多