所以我已将NSURL
转换为String
。
因此,如果我println
看起来像file:///Users/...
等。
稍后我希望将其作为NSURL
返回,所以我尝试将其转换回来,如下所示,但是我丢失了上面的字符串版本中出现的两个正斜杠,这反过来又破坏了代码url
无效。
为什么我的转换回NSURL
从我提供的String
中删除两个正斜杠,如何转换回包含三个正斜杠的NSURL
?
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
答案 0 :(得分:112)
在 Swift 4 和 Swift 3 中, 将字符串转换为网址:
URL(string: String)
,或者
URL.init(string: "yourURLString")
并将网址转换为字符串:
URL.absoluteString
下面的那个将网址的'内容'转换为字符串
String(contentsOf: URL)
答案 1 :(得分:99)
fileURLWithPath()
用于将纯文件路径(例如" / path / to / file")转换为URL。您的urlString
是包含该方案的完整网址字符串,因此您应该使用
let url = NSURL(string: urlstring)
将其转换回NSURL
。例如:
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
答案 2 :(得分:63)
有一种更好的方法可以从Swift中的NSURL获取路径的字符串版本:
let path:String = url.path
答案 3 :(得分:24)
更新:注意网址,在这个例子中可以是nil。 您可以将您的网址包装在引号中以将其转换为字符串
let urlString = "http://ifconfig.me"
//string to url
let url = NSURL(string: urlString)
//url to string
let string = "\(url)"
答案 4 :(得分:13)
Swift 3 (忘记NSURL)。
let fileName = "20-01-2017 22:47"
let folderString = "file:///var/mobile/someLongPath"
使用字符串制作网址:
let folder: URL? = Foundation.URL(string: folderString)
// Optional<URL>
// ▿ some : file:///var/mobile/someLongPath
如果我们要添加文件名。请注意,appendingPathComponent()会自动添加百分比编码:
let folderWithFilename: URL? = folder?.appendingPathComponent(fileName)
// Optional<URL>
// ▿ some : file:///var/mobile/someLongPath/20-01-2017%2022:47
当我们想要String但没有root部分时(注意自动删除百分比编码):
let folderWithFilename: String? = folderWithFilename.path
// ▿ Optional<String>
// - some : "/var/mobile/someLongPath/20-01-2017 22:47"
如果我们想要保留根部分,我们会这样做(但请注意编码百分比 - 它不会被删除):
let folderWithFilenameAbsoluteString: String? = folderWithFilenameURL.absoluteString
// ▿ Optional<String>
// - some : "file:///var/mobile/someLongPath/20-01-2017%2022:47"
手动添加字符串的百分比编码:
let folderWithFilenameAndEncoding: String? = folderWithFilename.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
// ▿ Optional<String>
// - some : "/var/mobile/someLongPath/20-01-2017%2022:47"
删除百分比编码:
let folderWithFilenameAbsoluteStringNoEncodig: String? = folderWithFilenameAbsoluteString.removingPercentEncoding
// ▿ Optional<String>
// - some : "file:///var/mobile/someLongPath/20-01-2017 22:47"
百分比编码很重要,因为网络请求的URL需要它们,而文件系统的URL永远不会起作用 - 这取决于使用它们的实际方法。需要注意的是,它们可能会被自动删除或添加,因此请仔细调试这些转换。
答案 5 :(得分:13)
let url = URL(string: "URLSTRING HERE")
let anyvar = String(describing: url)
答案 6 :(得分:8)
Swift 3版本代码:
let urlString = "file:///Users/Documents/Book/Note.txt"
let pathURL = URL(string: urlString)!
print("the url = " + pathURL.path)
答案 7 :(得分:2)
迅速5。
要将String
转换为URL
:
let stringToURL = URL(string: "your-string")
要将URL
转换为String
:
let urlToString = stringToURL?.absoluteString
答案 8 :(得分:1)
Swift 3与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
}
}
答案 9 :(得分:1)
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.absoluteString // "file:////Users/Me/Desktop/Doc.txt"
let c = "\(url)" // "file:////Users/Me/Desktop/Doc.txt"
let d = url.path // "/Users/Me/Desktop/Doc.txt"
// but d's value will be invisible due to debug process
let e = "\(url.path)" // "/Users/Me/Desktop/Doc.txt"
extension Optional where Wrapped == URL {
func toString() -> String {
guard let val = self else { return "" }
return "\(val.path)"
}
}
let str = optionalUrl.toString() // "/Users/Me/Desktop/Doc.txt"