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.
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.)