Ios Swift 3.0 FileManager.fileExists(路径:)始终返回false
当我使用方法Ios Swift 3.0 FileManager.fileExists(路径:)始终返回false,ios,swift,nsfilemanager,Ios,Swift,Nsfilemanager,当我使用方法.fileExists(atPath:)判断文件系统中是否存在该文件时,该方法总是向我返回false。我检查了文件系统,文件确实存在。这是我的密码: let filePath = url?.path var isDir : ObjCBool = false if(self.fileManager.fileExists(atPath: filePath!, isDirectory: &isDir)){ let result = NSData(contentsOfFil
.fileExists(atPath:)
判断文件系统中是否存在该文件时,该方法总是向我返回false。我检查了文件系统,文件确实存在。这是我的密码:
let filePath = url?.path
var isDir : ObjCBool = false
if(self.fileManager.fileExists(atPath: filePath!, isDirectory: &isDir)){
let result = NSData(contentsOfFile: filePath!)
}
或
if子句将始终被跳过
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
var path = paths[0] as String;
path = path + "/YourFilePath"
if((NSFileManager.defaultManager().fileExistsAtPath(path))) {
let result = NSData(contentsOfFile: filePath!)}
请尝试上面的代码并再次检查
expandingTildeInPath
展开路径李>
我假设您的
url
是url
类型。如果是,请尝试以下方法:
let filePath = url?.path // always try to work with URL when accessing Files
if(FileManager.default.fileExists(atPath: filePath!)){ // just use String when you have to check for existence of your file
let result = NSData(contentsOf: url!) // use URL instead of String
}
说够了,您应该像这样更改您的实现:
if(FileManager.default.fileExists(atPath: (url?.path)!)){ // just use String when you have to check for existence of your file
let result = NSData(contentsOf: url!) // use URL instead of String
}
编辑:1
还有更好的方法,你可以称之为快速方法(:D)。您不必显式检查文件是否存在
guard let result = NSData(contentsOf: fileURL) else {
// No data in your fileURL. So no data is received. Do your task if you got no data
// Keep in mind that you don't have access to your result here.
// You can return from here.
return
}
// You got your data successfully that was in your fileURL location. Do your task with your result.
// You can have access to your result variable here. You can do further with result constant.
print(result)
不带
Objective-C
ishNS
前缀的Swift 3.0+更新:
在swift 3中 以防有人像我一样困惑,以下是完整的片段:
let str = "file:///Users/martian2049/Library/Developer/CoreSimulator/Devices/67D744AA-6EEC-4AFD-A840-366F4D78A18C/data/Containers/Data/Application/DD96F423-AF9F-4F4D-B370-94ADE7D6D0A5/Documents/72b8b0fb-7f71-7f31-ac9b-f9cc95dfe90d.mp3"
let url = URL(string: str)
print(url!.path,"\n")
if FileManager.default.fileExists(atPath: url!.path) {
print("FILE Yes AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
这张照片
/Users/martian2049/Library/Developer/CoreSimulator/Devices/67D744AA-6EEC-4AFD-A840-366F4D78A18C/data/Containers/Data/Application/DD96F423-AF9F-4F4D-B370-94ADE7D6D0A5/Documents/72b8b0fb-7f71-7f31-ac9b-f9cc95dfe90d.mp3
FILE Yes AVAILABLE
请注意“
文件://
”是如何被截断的?我想分享我的经验,以防其他人对此感到困惑
在iOS 10-11、Xcode 9.2和Swift 3.2上测试
简短回答:如果您将文件路径保存到磁盘,您可以通过不在其中包含文档目录来解决此问题。
相反,每次需要使用保存的路径检索文件时,请获取Documents目录并附加路径
对于iOS应用程序,我通过相对URL(比如
URL
)将图像保存到…/Documents/Pictures。
当图像被保存时,一个路径,比如说url.path
,也被保存在一个核心数据实体中
后来我尝试通过FileManager.default.fileExists(atPath:url.path)
检索图像时,它总是返回false
我在iPhone上测试这个应用程序。事实证明,由于某种原因,每次我从Xcode运行应用程序时,应用程序标识符文件夹都会发生变化
因此:
- 应用程序从Xcode打开->图像保存->应用程序关闭->应用程序从物理设备打开-> fileExists->TRUE
- 应用程序从Xcode打开->图像保存->应用程序关闭->应用程序从Xcode打开->文件存在->错误
if(FileManager.default.fileExists(atPath: (url?.path)!)){ // just use String when you have to check for existence of your file
let result = NSData(contentsOf: url!) // use URL instead of String
}
- /var/mobile/Containers/Data/Application/5D4632AE-C432-4D37-A3F7-ECD05716AD8A/Documents
- /var/mobile/Containers/Data/Application/D09904C3-D80D-48EB-ACFB-1E42D878AFA4/Documents
你发现了问题。我也遇到了同样的问题,这对我来说很有效
filePath.replacingOccurrences(of: "file://", with: "")
只需使用path而不是absoluteString来删除文件://
FileManager.default.fileExists(atPath: URL.init(string: "your_url")!.path)
您是否指向文件所在的确切文件位置?filePath的值是多少?它是以~开头的吗?如果是这样,您首先需要呼叫扩展瓷砖在里面Path@user3608500是的,指向一个确切的文件,我想判断这个文件是否存在。@mlidal以“/Users/De…”开头可能路径不正确?请检查url1。路径是“/Users/De/Library/Developer/CoreSimulator/Devices/~~~~~~~/Documents/Cache/qbyNPuA.mp3”。2.这个文件是由代码编写的,所以我认为它是可以访问的。太棒了!在我的例子中,我使用的是
.absoluteString
而不是.path
。这个固定的itUsing.path是我的钥匙,而不是.absoluteString。老实说,如果我能回去,我会学习OC。swift不是一种常见的语言,它指出文件://
被截断是必不可少的。非常感谢。这对我来说是正确的解释。也许可以添加一些代码来保存和检索图像,以供其他寻求帮助的人使用。这解决了我的问题。更让人困惑的是,当你下载容器时,你看到文件就在那里。这应该是公认的答案。其他大多数答案都与我的情况相同。非常感谢。
FileManager.default.fileExists(atPath: URL.init(string: "your_url")!.path)