ios - 文件管理器检查 fileExists 是否在 iOS 13 中不起作用
问题描述
我有一个将图像存储到 documentDirectory 的应用程序。但是,每当我尝试检查文件是否存在于其路径中时,我总是会得到错误的响应。这在 iOS 13 之前从来都不是问题,但我根本无法加载图像,没有错误消息。
我像这样创建我的文件路径:
func cachePathWithName(name: String) -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let cachesPath: String = paths as String
let cachePath = cachesPath.stringByAppendingPathComponent(path: name)
createPathIfNecessary(path: cachesPath)
createPathIfNecessary(path: cachePath)
return cachePath
}
func getFilePathForURL(url: URL, folderName: String) -> String {
return cachePathWithName(name: folderName).stringByAppendingPathComponent(path: "\(url.hashValue)")
}
let filePath = getFilePathForURL(url: url1, folderName: "TILE_CACHE")
这就是我检查我的文件是否存在于路径中的方式:
if file.fileExists(atPath: filePath) {
}
解决方案
试试这个代码
let documentsURL = try! FileManager().url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
fileURL = documentsURL.appendingPathComponent(Your File Name)
if fileURL != nil{
let fileExists = FileManager().fileExists(atPath: (fileURL?.path)!)
print(fileExists)
if fileExists == true{
//File is Already in Local Storage
}
else{
//File is not in Local Storage
}
}
推荐阅读
- tensorflow - 如何知道 Tensorflow 实际“看到”了什么?
- php - 如何使插件内的变量可用于functions.php
- json - Spring Boot JSON 过滤器
- lambda - 使用python 2.7使用lambda函数停止AWS极光数据库
- c# - Android Unity 中语音命令的调用函数
- json - 管理来自服务器的响应类型
- cucumber-jvm - 我可以在黄瓜 Java 中运行/使用 3 个带有单步定义文件的功能文件吗
- java - 需要复制构造函数说明
- apache-kafka - 一位消费者宕机/崩溃时的 Kafka 消费者行为
- facebook - 是否可以将一个聊天机器人中的 user_id 与其他聊天应用程序一起使用?