将字符串转换为 NSURL,其中包含字符 '\\'
Convert string to NSURL with character '\\' inside
我用 Swift 2.0.
编码
我的 URL 字符串是这样的:
let urlString = "http://example.com/api/getfile/?filepath=C:\1.txt"
当我把它转换成NSURL时,它returns nil。
let OrginUrl = NSURL(string: urlString)
有人知道怎么做吗?
有两个问题需要解决:
为了包含 \
必须对其进行转义,因为它本身就是转义字符。
'\' 字符不允许出现在 URL 中,因此需要对它们进行 URL 编码
let urlString = "http://example.com/api/getfile/?filepath=C:\\1.txt"
print("urlString: \(urlString)")
var escapedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
print("escapedString!: \(escapedString!)")
let orginUrl = NSURL(string: escapedString!)
print("orginUrl: \(orginUrl!)")
urlString:
http://example.com/api/getfile/?filepath=C:\1.txt
escapedString!:
http%3A%2F%2Fexample.com%2Fapi%2Fgetfile%2F%3Ffilepath=C%3A%5C%5C1.txt
orginUrl:
http%3A%2F%2Fexample.com%2Fapi%2Fgetfile%2F%3Ffilepath=C%3A%5C%5C1.txt
你应该使用 unicode 而不是反斜杠两次。
stringByAddingPercentEscapesUsingEncoding
is deprecated: Use stringByAddingPercentEncodingWithAllowedCharacters(_:)
instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL components or subcomponents since each URL component or subcomponent has different rules for what characters are valid.
Documentation Reference
示例代码如下:
let myLink = "http://example.com/api/getfile/?filepath=C:\u{005C}\u{005C}1.txt"
var newLink = ""
if let queryIndex = myLink.characters.indexOf("?"){
newLink += myLink.substringToIndex(queryIndex.successor())
if let filePathIndex = myLink.characters.indexOf("=")?.successor() {
newLink += myLink.substringWithRange(queryIndex.successor()...filePathIndex.predecessor())
let filePath = myLink.substringFromIndex(filePathIndex)
if let pathEscaped = filePath.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet()) {
newLink += pathEscaped
}
}
}
if let newURL = NSURL(string: newLink) {
print(newURL, separator: "", terminator: "")
} else {
print("invalid")
}
结果你会得到:
我用 Swift 2.0.
编码我的 URL 字符串是这样的:
let urlString = "http://example.com/api/getfile/?filepath=C:\1.txt"
当我把它转换成NSURL时,它returns nil。
let OrginUrl = NSURL(string: urlString)
有人知道怎么做吗?
有两个问题需要解决:
为了包含 \
必须对其进行转义,因为它本身就是转义字符。
'\' 字符不允许出现在 URL 中,因此需要对它们进行 URL 编码
let urlString = "http://example.com/api/getfile/?filepath=C:\\1.txt"
print("urlString: \(urlString)")
var escapedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
print("escapedString!: \(escapedString!)")
let orginUrl = NSURL(string: escapedString!)
print("orginUrl: \(orginUrl!)")
urlString:
http://example.com/api/getfile/?filepath=C:\1.txtescapedString!:
http%3A%2F%2Fexample.com%2Fapi%2Fgetfile%2F%3Ffilepath=C%3A%5C%5C1.txtorginUrl:
http%3A%2F%2Fexample.com%2Fapi%2Fgetfile%2F%3Ffilepath=C%3A%5C%5C1.txt
你应该使用 unicode 而不是反斜杠两次。
stringByAddingPercentEscapesUsingEncoding
is deprecated: UsestringByAddingPercentEncodingWithAllowedCharacters(_:)
instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL components or subcomponents since each URL component or subcomponent has different rules for what characters are valid.Documentation Reference
示例代码如下:
let myLink = "http://example.com/api/getfile/?filepath=C:\u{005C}\u{005C}1.txt"
var newLink = ""
if let queryIndex = myLink.characters.indexOf("?"){
newLink += myLink.substringToIndex(queryIndex.successor())
if let filePathIndex = myLink.characters.indexOf("=")?.successor() {
newLink += myLink.substringWithRange(queryIndex.successor()...filePathIndex.predecessor())
let filePath = myLink.substringFromIndex(filePathIndex)
if let pathEscaped = filePath.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet()) {
newLink += pathEscaped
}
}
}
if let newURL = NSURL(string: newLink) {
print(newURL, separator: "", terminator: "")
} else {
print("invalid")
}
结果你会得到: