Swift 2.0中的NSFileManager fileExistsAtPath?

问题描述 投票:1回答:2

因此,使用Swift 2.0,看起来苹果正在引导我们使用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,查看用户的Documents目录?

ios swift2 nsfilemanager
2个回答
5
投票

没关系,其中一个“五秒钟后”答案。

我可以使用fileExistsAtPath(theURL.path)来做到这一点。当然,首先检查path是否为非零值!


0
投票

Swift 4版本:

FileManager.default.fileExists(atPath: url.path)
© www.soinside.com 2019 - 2024. All rights reserved.