Swift-编码URL

Swift-编码URL,第1张

Swift-编码URL 迅捷3

在Swift 3中有

addingPercentEncoding

let originalString = "test/test"let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)print(escapedString!)

输出

测试%2Ftest

斯威夫特1

在iOS 7及更高版本中,

stringByAddingPercentEncodingWithAllowedCharacters

var originalString = "test/test"var escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())println("escapedString: (escapedString)")

输出:

测试%2Ftest

以下是有用的(倒置)字符集

URLFragmentAllowedCharacterSet  "#%<>[]^`{|}URLHostAllowedCharacterSet      "#%/<>?@^`{|}URLPasswordAllowedCharacterSet  "#%/:<>?@[]^`{|}URLPathAllowedCharacterSet      "#%;<>?[]^`{|}URLQueryAllowedCharacterSet     "#%<>[]^`{|}URLUserAllowedCharacterSet      "#%/:<>?@[]^`

如果要转义其他字符集,请创建一个字符集:
添加了“ =”字符的示例:

var originalString = "test/test=42"var customAllowedSet =  NSCharacterSet(charactersInString:"="#%/<>?@\^`{|}").invertedSetvar escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters(customAllowedSet)println("escapedString: (escapedString)")

输出:

测试%2Ftest%3D42

验证不在集合中的ASCII字符的示例:

func printCharactersInSet(set: NSCharacterSet) {    var characters = ""    let iSet = set.invertedSet    for i: UInt32 in 32..<127 {        let c = Character(UnipreScalar(i))        if iSet.longCharacterIsMember(i) { characters = characters + String(c)        }    }    print("characters not in set: '(characters)'")}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5662446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存