【问题标题】:NSFileManager fileExistsAtPath in Swift 2.0?Swift 2.0 中的 NSFileManager fileExistsAtPath?
【发布时间】:2015-12-18 15:43:00
【问题描述】:

因此,使用 Swift 2.0,Apple 似乎正在引导我们使用 NSURL 而不是 NSString 作为路径。

我正在尝试确定一个文件是否存在于 iOS 中用户的 Documents 目录中,但我无法将其拼凑起来。

如果我使用以下内容,Swift 2.0 会抱怨我不应该使用 stringByAppendingPathComponent,而应该使用 URL。

let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let dbPath = documents.stringByAppendingPathComponent(“Whatever.sqlite”)

如果我得到 URL,像这样:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("Retrollect.sqlite”)

然后我无法调用NSFileManager.defaultManager().fileExistsAtPath 来确定文件是否存在。

是否有与fileExistsAtPath() 等效的NSURL,用于查看用户的文档目录?

【问题讨论】:

    标签: ios swift2 nsfilemanager


    【解决方案1】:

    没关系,“五秒​​后”的答案之一。

    我可以使用fileExistsAtPath(theURL.path) 来执行此操作。当然,首先检查path 是否为非零!

    【讨论】:

      【解决方案2】:

      Swift 4 版本:

      FileManager.default.fileExists(atPath: url.path)
      

      【讨论】:

        猜你喜欢
        • 2014-12-13
        • 2015-11-30
        • 2011-04-05
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        • 2014-09-01
        • 1970-01-01
        相关资源
        最近更新 更多