快速编码URL如果我像这样编码一个字符串:var escapedString = originalString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)它逃不出斜杠/.我搜索并找到了这个目标C代码:NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );是否有一种更简单的方式来编码一个URL,如果不是,我如何用SWIFT写这个呢?
3 回答
皈依舞
TA贡献1851条经验 获得超3个赞
SWIFT 3
addingPercentEncoding
let originalString = "test/test"let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)print(escapedString!)
测试%2 Ftest
SWIFT 1
stringByAddingPercentEncodingWithAllowedCharacters
var originalString = "test/test"var escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters (.URLHostAllowedCharacterSet())println("escapedString: \(escapedString)")
测试%2 Ftest
URLFragmentAllowedCharacterSet "#%<>[\]^`{|} URLHostAllowedCharacterSet "#%/<>?@\^`{|}URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|} URLPathAllowedCharacterSet "#%;<>?[\]^`{|}URLQueryAllowedCharacterSet "#%<>[\]^`{|} URLUserAllowedCharacterSet "#%/:<>?@[\]^`
var originalString = "test/test=42"var customAllowedSet = NSCharacterSet(charactersInString:"=\"#%/<>?@\\^`{|}"). invertedSetvar escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters(customAllowedSet)println ("escapedString: \(escapedString)")
测试%2 Ftest%3D42
func printCharactersInSet(set: NSCharacterSet) { var characters = "" let iSet = set.invertedSet for i: UInt32 in 32..<127 { let c = Character(UnicodeScalar(i)) if iSet.longCharacterIsMember(i) { characters = characters + String(c) } } print("characters not in set: \'\(characters)\'")}
- 3 回答
- 0 关注
- 740 浏览
添加回答
举报
0/150
提交
取消