ios - 文件管理器检查 fileExists 是否在 iOS 13 中不起作用

标签 ios swift

<分区>

我有一个将图像存储到 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
                }
            }

关于ios - 文件管理器检查 fileExists 是否在 iOS 13 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070559/

上一篇:ios - 无法使用 AWS S3 iOS SDK 从存储桶的子文件夹下载

下一篇:ios - NSFetchedResultsController 'didChange' 使用新的 CollectionDiffing 会导致奇怪的更改操作,从而导致崩溃

相关文章:

ios - 创建 Apple App ID 时,权利 "Data Protection"有什么用?

ios - 这是在说什么布局约束?

ios - 在ios中获取正确的状态栏颜色

objective-c - 如何隐藏表索引和表?

ios - 如何在 SplitView Controller 中隐藏和取消隐藏主视图 Controller

ios - 为了在 VoiceOver 打开时调用 accessibilityIncrement/accessibilityDecrement,我需要做什么特别的事情吗?

ios - UITableView 不显示内容,除非滚动或触摸单元格

ios - Swift UICollectionView 委托(delegate)执行 segue

swift - XCUITest如何获取SkyFloatingLabelTextField中 float 标题的内容?

swift - EKEvent的镜像不显示数据