I have the following issue with a whitespace in a local file path: when setting the file name in an unescaped string, everything works perfectly:
var path = "/absolute Path/testfile.txt"
let url = URL(fileURLWithPath: path)
if let returnstring = try? String(contentsOf: url, encoding: .utf8) {
return returnstring
} else {
return "could not open"
}
However, if I get the filename from
fileManager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
The whitespace is escaped with percent encoding
file:///absolute Path/testfile.txt```and the file is not opened anymore.
Any suggestions are much appreciated!
CodePudding user response:
The issue there is that you are getting your fileURL path using the URL absoluteString
property which contains a scheme and also percent encode to your string. What you need is to get the url path
property which does NOT contain a scheme and does NOT percent encode your fileURL path.