我有一个.mp4视频保存在Firebase中,我想将其保存到.cachesDirectory中。我想确保它已保存并尝试使用do-try块,但它不接受它:

在try表达式中不会发生对throwing函数的调用。

我还要如何检查文件是否已成功保存到.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")
}

最佳答案

使用不带write(toFile:)参数的automically方法。

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

10-08 05:23