如何在 Swift 2 中编码特殊字符,如 ' (NSWindow CP1250 String Encoding)
How to encode special character like ’ (NSWindowsCP1250StringEncoding) in Swift2
与 Swift2
中一样,已弃用 stringByAddingPercentEscapesUsingEncoding()
而不是使用 stringByAddingPercentEncodingWithAllowedCharacters()
。
但是swift2
中如何对' % & 这样的特殊字符进行编码
例如,在 iOS8
(swift1.2) 中,我使用以下代码进行编码
NSURL(string: "myurl.php?deviceName=name’phone".stringByAddingPercentEscapesUsingEncoding(NSWindowsCP1250StringEncoding)!)
它工作正常,即在服务器上它正确解码。
但在 iOS9
(Swift2.0) 中我使用了以下代码
NSURL(string: "myurl.php?deviceName=name ’phone".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)
它不会正确解码。
请告诉我如何在 swift2.0 中编码特殊字符?
编辑:
Eric D 的回答是正确的,但是当我在 stringURL
以下编码时,它不会正确编码。
为什么?
let stringURL = "https://my.server.com/login.php?e=email&dn=my’s iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D24&tz=330"
print(stringURL)
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)!
print(url) //https%253A//my.server.com/login.php%253Fe=email&dn=my%25E2%2580%2599s%2520iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D ... 4&tz=330
编辑 2:
如何在 swift2.0 中编码 NSWindowsCP1250StringEncoding ?
使用URLPathAllowedCharacterSet()
作为字符集:
let stringURL = "myurl.php?deviceName=name'phone"
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded)!
print(url) // "myurl.php%3FdeviceName=name'phone"
注意 '
不需要编码,它是一个有效的 URL 字符。
更新: 在评论中您声明它仍然无法正常工作,因为您的编码 URL 被截断并包含 ...
,但实际上这很可能只是被 NSURL 截断的 印刷说明; NSURL 对象包含的实际 URL 应该没问题。否则它将是零。您应该检查服务器端是否存在很长但正确的 URLs.
的问题
let newURLEncodedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
与 Swift2
中一样,已弃用 stringByAddingPercentEscapesUsingEncoding()
而不是使用 stringByAddingPercentEncodingWithAllowedCharacters()
。
但是swift2
例如,在 iOS8
(swift1.2) 中,我使用以下代码进行编码
NSURL(string: "myurl.php?deviceName=name’phone".stringByAddingPercentEscapesUsingEncoding(NSWindowsCP1250StringEncoding)!)
它工作正常,即在服务器上它正确解码。
但在 iOS9
(Swift2.0) 中我使用了以下代码
NSURL(string: "myurl.php?deviceName=name ’phone".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)
它不会正确解码。
请告诉我如何在 swift2.0 中编码特殊字符?
编辑:
Eric D 的回答是正确的,但是当我在 stringURL
以下编码时,它不会正确编码。
为什么?
let stringURL = "https://my.server.com/login.php?e=email&dn=my’s iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D24&tz=330"
print(stringURL)
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)!
print(url) //https%253A//my.server.com/login.php%253Fe=email&dn=my%25E2%2580%2599s%2520iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D ... 4&tz=330
编辑 2:
如何在 swift2.0 中编码 NSWindowsCP1250StringEncoding ?
使用URLPathAllowedCharacterSet()
作为字符集:
let stringURL = "myurl.php?deviceName=name'phone"
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded)!
print(url) // "myurl.php%3FdeviceName=name'phone"
注意 '
不需要编码,它是一个有效的 URL 字符。
更新: 在评论中您声明它仍然无法正常工作,因为您的编码 URL 被截断并包含 ...
,但实际上这很可能只是被 NSURL 截断的 印刷说明; NSURL 对象包含的实际 URL 应该没问题。否则它将是零。您应该检查服务器端是否存在很长但正确的 URLs.
let newURLEncodedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())