Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported."

Hello, I'm trying to save some Strings in a csv document:

        var tempString = String()
        for Rezept in alleRezepte {
            tempString += "\(Rezept.name), \(Rezept.description), \(Rezept.nutrients), \(Rezept.whatToDo)\n"
        }
        let dirs = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true) as? [String])
        let path = dirs?[0].appending("rezepte.csv")
        let url = URL(string: path!)
        do {
            try tempString.write(to: url!, atomically: true, encoding: String.Encoding.utf8)
        } catch {
            print(error)
        }
        print("Daten gesichert")


And I get this error:

CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme

Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported."


How can I fix this?


Thank you for helping.

Answered by QuinceyMorris in 211587022

At the very least, you have a problem in line 6, because you are not ensuring there is a "/" separator between path components. There may also be an issue with a returned path starting with a "~". If it isn't one of those things, then print out the path to see what's wrong with it.


However, the larger issue is that you should stop using path strings at all. There is a FileManager method "urls(for:in:)" that returns URLs directly, and can you the URL addPathComponent method to build the final URL properly.


(It's not that you can't use paths, just that URLs are always preferred these days.)

Accepted Answer

At the very least, you have a problem in line 6, because you are not ensuring there is a "/" separator between path components. There may also be an issue with a returned path starting with a "~". If it isn't one of those things, then print out the path to see what's wrong with it.


However, the larger issue is that you should stop using path strings at all. There is a FileManager method "urls(for:in:)" that returns URLs directly, and can you the URL addPathComponent method to build the final URL properly.


(It's not that you can't use paths, just that URLs are always preferred these days.)

What QuinceyMorris said but also…

The specific cause of your problem is line 7. If you want to go from a path to a file URL, you need to use

URL(fileURLWithPath:)
. What you have,
URL(string:)
, creates a URL with a path component but no scheme, and that’s what’s triggering the specific error you’ve got.

QuinceyMorris’s advice is correct though: life is much easier if you go ‘all in’ on URLs. I just posted an example on your other thread.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

If I change line 7 to:

let url = URL(fileURLWithPath: path)


I get this new error:

Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “Documentsrezepte.csv” in the folder “7628BDA0-E8BD-4058-B49F-9A3B70F1FC02”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/7628BDA0-E8BD-4058-B49F-9A3B70F1FC02/Documentsrezepte.csv, NSUnderlyingError=0x170055c00 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Thanks for the advice to stop using path strings. I'm just getting started with swift programming.

I tried it with the FileManager, but don't get it working.

Could you show me an example of how I'm doing it, so that I can save some Strings in a .csv document?


Mine looks like that:

let dirs = FileManager.default(NSSearchPathForDirectoriesInDomains(for: .documentDirectory, in: .allDomainsMask, true))

I get this error: Cannot call value of non-function type 'FileManager'


That would help me a lot. Thanks.

Okay, I got it! No more error!


My code looks like that:


var fileName = "rezepte.csv"
var tempString = String()
        for Rezept in alleRezepte {
            tempString += "\(Rezept.name), \(Rezept.description), \(Rezept.nutrients), \(Rezept.whatToDo)\n"
}


let docDirURL = try! FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true)
let fileURL = docDirURL.appendingPathComponent(fileName)

try! tempString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)

print("Daten gesichert")


Thank you both QuinceyMorris and eskimo!

This absolutely solved my issue. Thanks.


-

Soumalya

Thank you! Works!

:)

Your issue is that URL(string:) expects a valid URL with a scheme (like file://), but you're passing a file path directly.

It is similar to the following error:

Instead, you should use URL(fileURLWithPath:) for file paths. Here's the fixed code:

var tempString = String()

for Rezept in alleRezepte {

    tempString += "\(Rezept.name), \(Rezept.description), \(Rezept.nutrients), \(Rezept.whatToDo)\n"

}

if let dirs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {

    let path = dirs.appending("/rezepte.csv")

    let url = URL(fileURLWithPath: path)

    do {

        try tempString.write(to: url, atomically: true, encoding: .utf8)

        print("Daten gesichert")

    } catch {

        print("Error saving file: \(error)")

    }

}
Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported."
 
 
Q