Swift -How to know if a file was successfully saved in .cachesDirectory

白昼怎懂夜的黑 提交于 2020-08-10 01:19:48

问题


I have a .mp4 video saved in Firebase and I want to save it to the .cachesDirectory. I want to make sure it is saved and tried to use a do-try block but it wouldn't accept it:

No calls to throwing functions occur within 'try' expression.

How else can I check to see if the file was successfully saved to the .cachesDirectory?

let fbStr = "https://firebasestorage.googleapis.com/v0/b/myApp.appspot.com/o/abcd%277920FHqFBkl7D6j%2F-MC65EFG_qT0KZbdtFhU%2F48127-8C29-4666-96C9-E95BE178B268.mp4?alt=media&token=bf85dcd1-8cee-428e-87bc-91800b7316de"
guard let url = URL(string: fbStr) else { return }
let videoData = NSData(contentsOf: url)
let cachesDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!

do {
    try videoData?.write(toFile: cachesDir.path, atomically: true)
} catch {
    print("video wasn't saved to cachesDirectory") 
}

回答1:


Use the write(toFile:) method without automically parameter.

do {
    try videoData?.write(toFile: cachesDir.path, options: .atomic)
} catch {
    print("video wasn't saved to cachesDirectory")
}


来源:https://stackoverflow.com/questions/62873052/swift-how-to-know-if-a-file-was-successfully-saved-in-cachesdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!