So I have converted an NSURL
to a String
.
So if I println
it looks like file:///Users/...
etc.
Later I want this
2020 | SWIFT 5.1:
let url = URL(fileURLWithPath: "//Users/Me/Desktop/Doc.txt")
let a = String(describing: url) // "file:////Users/Me/Desktop/Doc.txt"
let b = "\(url)" // "file:////Users/Me/Desktop/Doc.txt"
let c = url.absoluteString // "file:////Users/Me/Desktop/Doc.txt"
let d = url.path // "/Users/Me/Desktop/Doc.txt"
BUT value of d
will be invisible due to debug process, so...
THE BEST SOLUTION:
let e = "\(url.path)" // "/Users/Me/Desktop/Doc.txt"
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
}
}
let url = URL(string: "URLSTRING HERE")
let anyvar = String(describing: url)
Swift 3 version code:
let urlString = "file:///Users/Documents/Book/Note.txt"
let pathURL = URL(string: urlString)!
print("the url = " + pathURL.path)
NOTICE: pay attention to the url, it's optional
and it can be nil
.
You can wrap your url in the quote to convert it to a string. You can test it in the playground.
Update for Swift 5, Xcode 11:
import Foundation
let urlString = "http://ifconfig.me"
// string to url
let url = URL(string: urlString)
//url to string
let string = "\(url)"
// if you want the path without `file` schema
// let string = "\(url.path)"
There is a nicer way of getting the string version of the path from the NSURL in Swift:
let path:String = url.path