I've got a file path /Users/alexandra/Downloads/folder with spaces/
and I want to remove the percent encoding and make it a file URL. Using the method stringByRemovingPercentEncoding
makes the string null.
The documentation says "A new string with the percent-encoded sequences removed, or nil if the receiver contains an invalid percent-encoding sequence.", but I don't see
for a space being wrong?
CodePudding user response:
You didn't show us your non-working code in Objective-C, but this works fine on my machine (in Swift):
if let path = "/Users/alexandra/Downloads/folder with spaces/".removingPercentEncoding {
let url = URL(fileURLWithPath: path)
print(url) // file:///Users/alexandra/Downloads/folder with spaces/
}
On the other hand, since you've already wrongly acquired percent encoding in a string pathname, why not just stick file://
in front of it and be done with it?
let path = "/Users/alexandra/Downloads/folder with spaces/"
if let url = URL(string: "file://" path) {
print(url)
}