簡體   English   中英

FileManager.createDirectory失敗,出現NSCocoaErrorDomain代碼:518

[英]FileManager.createDirectory fails with NSCocoaErrorDomain Code: 518

我正在做

    let tempDirectory = URL(string: "\(NSTemporaryDirectory())video/")!
    do {
        try FileManager.default.createDirectory(
            at: tempDirectory,
            withIntermediateDirectories: true)
    } catch { report(error) }

而這通常會拋出一個NSCocoaErrorDomain代碼:518。

任何想法的原因? 我認為可能因為那里已有東西,所以我補充道

    var isDir: ObjCBool = false
    if FileManager.default.fileExists(
        atPath: tempDirectory.absoluteString,
        isDirectory: &isDir
    ) {
        if isDir.boolValue {
            print("Temp directory exists on launch")
        }
        else {
            print("Temp directory exists on launch and is a file")
        }
        return
    }

但這似乎沒有抓到任何東西

您的tempDirectory構建不正確。 你要:

let tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory()). appendingPathComponent("video")

您的代碼的問題是您沒有將值URL字符串傳遞給URL(string:) 由於您有文件路徑,因此需要使用URL(fileURLWithPath:) 並使用提供的方法構建路徑/ URL,以確保正確添加斜杠和其他部分。

從原始代碼打印tempDirectory的值,然后從我的答案中的代碼中打印新值。 注意關鍵區別。

您的網址將是:

在/ var / ...

它可能在“視頻”之前缺少斜線。

正確的文件URL將是這樣的:

文件:/// VAR / ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM