所以我将
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
在 Swift 5、Swift 4 和 Swift 3 将字符串转换为 URL:
URL(string: String)
或者,
URL.init(string: "yourURLString")
并将 URL 转换为字符串:
URL.absoluteString
下面的将url的“内容”转换为字符串
String(contentsOf: URL)
fileURLWithPath()
用于将普通文件路径(例如“/path/to/file”)转换为 URL。您的 urlString
是包含方案的完整 URL 字符串,因此您应该使用
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
有一种更好的方法可以从 Swift 中的 NSURL 获取路径的字符串版本:
let path:String = url.path
2021 | SWIFT 5.1:
字符串 --> URL :
let url1 = URL(fileURLWithPath: "//Users/Me/Desktop/Doc.txt")
let url2 = URL(fileURLWithPath: "//Users/Me/Desktop", isDirectory: true)
// !!!!!NEVER DO THIS FOR LOCAL PATHS!!!!!!
let url3 = URL(string: "file:///Users/Me/Desktop/Doc.txt")!
// !!!!!NEVER DO THIS!!!!!!
URL --> 字符串:
let a = String(describing: url1) // "file:////Users/Me/Desktop/Doc.txt"
let b = "\(url1)" // "file:////Users/Me/Desktop/Doc.txt"
let c = url1.absoluteString // "file:////Users/Me/Desktop/Doc.txt"
// Best solution in most cases
let d = url1.path // "/Users/Me/Desktop/Doc.txt"
字符串 --> URL :
let url = URL(string: "https://stackoverflow.com/questions/27062454/converting-url-to-string-and-back-again")!
URL --> 字符串:
url.absoluteString // https://stackoverflow.com/questions/27062454/converting-url-to-string-and-back-again
url.path // /questions/27062454/converting-url-to-string-and-back-again
注意:注意网址,它是
optional
,也可以是nil
。
您可以将 url 括在引号中以将其转换为字符串。你可以在操场上测试一下。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
let url = URL(string: "URLSTRING HERE")
let anyvar = String(describing: url)
Swift 3(忘记 NSURL)。
let fileName = "20-01-2017 22:47"
let folderString = "file:///var/mobile/someLongPath"
要从字符串中创建 URL:
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 但没有根部分时(注意百分比编码会自动删除):
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 并不总是有效 - 这取决于使用它们的实际方法。这里需要注意的是,它们可能会被自动删除或添加,因此最好仔细调试这些转换。
Swift 3 版本代码:
let urlString = "file:///Users/Documents/Book/Note.txt"
let pathURL = URL(string: urlString)!
print("the url = " + pathURL.path)
斯威夫特 5。
要将
String
转换为 URL
:
let stringToURL = URL(string: "your-string")
要将
URL
转换为 String
:
let urlToString = stringToURL?.absoluteString
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
}
}