Swift-如何知道文件是否成功保存在.cachesDirectory中 [英] Swift -How to know if a file was successfully saved in .cachesDirectory

查看:76
本文介绍了Swift-如何知道文件是否成功保存在.cachesDirectory中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

在"try"表达式中没有对throwing函数的调用.

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

我还能如何检查文件是否已成功保存到 .cachesDirectory ?

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") 
}

推荐答案

使用 write(toFile:)方法,而无需 automatic 参数.

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")
}

这篇关于Swift-如何知道文件是否成功保存在.cachesDirectory中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆