Converting URL to String and back again

Gary Simpson :

So I have converted an NSURL to a String. So if I println it looks like file:///Users/... etc.

Later I want this back as an NSURL so I try and convert it back as seen below, but I lose two of the forward slashes that appear in the string version above, that in turn breaks the code as the url is invalid.

Why is my conversion back to NSURL removing two forward slashes from the String I give it, and how can I convert back to the NSURL containing three forward slashes?

var urlstring: String = recordingsDictionaryArray[selectedRow]["path"] as String
println("the url string = \(urlstring)")
// looks like file:///Users/........etc
var url = NSURL.fileURLWithPath(urlstring)
println("the url = \(url!)")
// looks like file:/Users/......etc
Martin R :

fileURLWithPath() is used to convert a plain file path (e.g. "/path/to/file") to an URL. Your urlString is a full URL string including the scheme, so you should use

let url = NSURL(string: urlstring)

to convert it back to NSURL. Example:

let urlstring = "file:///Users/Me/Desktop/Doc.txt"
let url = NSURL(string: urlstring)
println("the url = \(url!)")
// the url = file:///Users/Me/Desktop/Doc.txt

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting string of an array back to array

converting RegExp to String then back to RegExp

Converting from JSONArray to String then back again

Java converting int to hex and back again

Converting char array into byte array and back again

Converting file into Base64String and back again

Converting NSString to NSDate (and back again)

Ruby string to rust and back again

Converting datetimes to timestamps and back again

Converting a byte array to string and back

Converting byte array to string and back again

Converting MPFR numbers to string and back

Swift: converting String to Float and back to String again after doing some mathematical operations

Converting String Array Back to an Array

Converting string to integer and then converting back to string

Converting a pandas Interval into a string (and back again)

Converting string to int and then back to string

Why converting a string path to Geometry and back again throws a FormatException?

converting byte to string and back again

Converting a string back into an array

Converting an array to string and back

Converting InkCanvas Strokes to a Byte Array and back again

Converting tree view to byte[], then back again

Swift - Font size increases when converting html string to attributed string and back again

Converting a string into binary and back

Javascript - Converting a String to an Array and Back Again within Chained Map Methods

Converting binary to string, then back again using python

Compressing or converting game data to a short string password (and back again) in Javascript

Converting raster to a dataframe, and then back to a raster again? [R]