Convert URL to string and vice versa

So, I converted NSURL to String . Therefore, if I println looks like file:///Users/... etc.

Later I want it to be like NSURL , so I try to convert it as shown below, but I lose two of the slashes that appear in the string version above, which in turn interrupts the code as the url invalid.

Why is my conversion going back to NSURL removing two slashes from String that I give it, and how can I convert back to NSURL containing three 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 
+56
swift nsurl
Nov 21 '14 at 13:23
source share
7 answers

fileURLWithPath() used to convert the path of a regular file (for example, "/ path / to / file") to a URL. Your urlString is a complete URL string, including a 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 
+68
Nov 21 '14 at 13:35
source share

There is a more convenient way to get a string version of a path from NSURL to Swift:

 let path:String = url.path 
+48
May 14 '16 at 14:45
source share

In Swift 4 and Swift 3 To convert a string to a URL:

 URL(string: String) 

or,

 URL.init(string: "yourURLString") 

And to convert the URL to String:

 URL.absoluteString 

The following converts the "content" URL to a string

 String(contentsOf: URL) 
+35
Oct 10 '16 at 22:55
source share

UPDATE: pay attention to the url, in this example it could be zero. You can transfer your url to a quote to convert it to a string

 let urlString = "http://ifconfig.me" //string to url let url = NSURL(string: urlString) //url to string let string = "\(url)" 
+18
Jul 23 '15 at 11:35
source share

Swift 3 (forget about NSURL).

 let fileName = "20-01-2017 22:47" let folderString = "file:///var/mobile/someLongPath" 

To make a URL from a string:

 let folder: URL? = Foundation.URL(string: folderString) // Optional<URL> // β–Ώ some : file:///var/mobile/someLongPath 

If we want to add a file name. Note that appendingPathComponent () automatically adds percent encoding:

 let folderWithFilename: URL? = folder?.appendingPathComponent(fileName) // Optional<URL> // β–Ώ some : file:///var/mobile/someLongPath/20-01-2017%2022:47 

If we want to have a String, but without the root part (note that percent encoding is deleted automatically):

 let folderWithFilename: String? = folderWithFilename.path // β–Ώ Optional<String> // - some : "/var/mobile/someLongPath/20-01-2017 22:47" 

If we want to keep the root part, we do it (but remember the percentage encoding - it is not deleted):

 let folderWithFilenameAbsoluteString: String? = folderWithFilenameURL.absoluteString // β–Ώ Optional<String> // - some : "file:///var/mobile/someLongPath/20-01-2017%2022:47" 

To manually add a percent encoding for a string:

 let folderWithFilenameAndEncoding: String? = folderWithFilename.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) // β–Ώ Optional<String> // - some : "/var/mobile/someLongPath/20-01-2017%2022:47" 

To remove percentage encoding:

 let folderWithFilenameAbsoluteStringNoEncodig: String? = folderWithFilenameAbsoluteString.removingPercentEncoding // β–Ώ Optional<String> // - some : "file:///var/mobile/someLongPath/20-01-2017 22:47" 

Percent encoding is important because it requires URLs for network requests, and URLs in the file system do not always work - it depends on the actual method that uses them. The danger here is that they can be deleted or added automatically, so it’s better to debug these conversions.

+8
Jan 20 '17 at 21:40
source share

Swift 3 Version Code:

 let urlString = "file:///Users/Documents/Book/Note.txt" let pathURL = URL(string: urlString)! print("the url = " + pathURL.path) 
+6
Feb 14 '17 at 21:22
source share

Swift 3 used with UIWebViewDelegate shouldStartLoadWith

  func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { let urlPath: String = (request.url?.absoluteString)! print(urlPath) if urlPath.characters.last == "#" { return false }else{ return true } } 
+1
Aug 25 '17 at 10:09 on
source share



All Articles