【问题标题】:How can i check that is there any existing file is there from nsfilemanager?我如何检查 nsfilemanager 中是否存在任何现有文件?
【发布时间】:2018-02-02 21:33:11
【问题描述】:

//这里frit我想在保存之前保存一个mp4文件我想检查它是否已经存在了。如果没有创建另一个名称

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let  savePathUrl = NSURL(string: ("\(documentsURL.appendingPathComponent("temp"))" + ".mp4"))
if FileManager.default.fileExists(atPath: **savePathUrl**)// here i get error
    }

{
    do

    {
        try FileManager.default.removeItem(atPath: **savePathUrl**)// here i get error
    }
    catch { }

}

【问题讨论】:

标签: ios swift


【解决方案1】:

首先,您创建savePathUrl 的方式毫无意义。替换:

let  savePathUrl = NSURL(string: ("\(documentsURL.appendingPathComponent("temp"))" + ".mp4"))

与:

let savePathUrl = documentsURL.appendingPathComponent("temp.mp4")

然后对于两个FileManager 管理器方法,您需要一个文件路径。要从文件 URL 中获取文件路径,您需要使用 path 属性。

if FileManager.default.fileExists(atPath: savePathUrl.path) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2015-03-05
    • 2011-10-09
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多