本文主要是介绍[Swift]URL编码|CharacterSet字符集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
URL编码
import UIKitclass SchoolDetailVC: BaseController {var webView: WKWebView!override func viewDidLoad() {super.viewDidLoad()}/// 自定义字符集private func loadRequsetOne(with openUrl: String) {let charSet = CharacterSet.urlQueryAllowed as NSCharacterSetlet mutSet = charSet.mutableCopy() as! NSMutableCharacterSetmutSet.addCharacters(in: "!*'();:@&=+$,/?%#[]")let encodingURL = openUrl.addingPercentEncoding(withAllowedCharacters: mutSet as CharacterSet)!if let url = URL(string: encodingURL) {webView.load( URLRequest(url: url))} else {}}/// 系统提供的字符集private func loadRequsetTwo(with openUrl: String) {let encodingURL = openUrlif (verifyUrl(urlString: openUrl) == false) {encodingURL = openUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!}if let url = URL(string: encodingURL) {webView.load( URLRequest(url: url))} else {}}// 验证URL的有效性func verifyUrl (urlString: String?) -> Bool {if let urlString = urlString {if let url = NSURL(string: urlString) {return UIApplication.shared.canOpenURL(url as URL)}}return false}}
CharacterSet(字符集)
CharacterSet.alphanumerics // 字母和数字的组合,包含大小写, 不包含小数点
CharacterSet.decimalDigits // 0-9的数字,也不包含小数点
CharacterSet.controlCharacters // ASCII 码0-31号字符,详见http://ascii.cl/control-characters.htmCharacterSet.whitespaces // 空格
CharacterSet.whitespacesAndNewlines // 空格和换行
CharacterSet.letters // 所有英文字母,包含大小写 65-90 97-122
CharacterSet.lowercaseLetters // 小写英文字母 97-122
CharacterSet.uppercaseLetters // 大写英文字母 65-90// 通用字符类别划分详见 https://msdn.microsoft.com/zh-cn/library/20bw873z(v=vs.110).aspxCharacterSet.nonBaseCharacters // Returns a character set containing the characters in Unicode General Category M*.
CharacterSet.decomposables // 没搞懂,也没用过,同音字母可以用这个?
CharacterSet.illegalCharacters // 不合规字符,没有在Unicode 3.2 标准中定义的字符
CharacterSet.punctuationCharacters // 标点符号,连接线,引号什么的 P*
CharacterSet.symbols // 符号,包含S* 所有内容,运算符,货币符号什么的
CharacterSet.capitalizedLetters // 字母,首字母大写,Lt类别
CharacterSet.newlines // 返回一个包含换行符的字符集,`U+000A ~ U+000D`, `U+0085`, `U+2028`, and `U+2029`
CharacterSet.urlHostAllowed // URL 中Host子模块中允许的字符集. "#%/<>?@\^`{|}"
CharacterSet.urlPathAllowed // URL 中domain后面的路径子模块中允许的字符集. "#%;<>?[\]^`{|}"
CharacterSet.urlUserAllowed // URL 中用户子模块中允许的字符集. "#%/:<>?@[\]^`"
CharacterSet.urlQueryAllowed // URL中请求信息子模块中允许的字符集. "#%<>[\]^`{|}"
CharacterSet.urlFragmentAllowed // 片段URL子模块中允许的字符集. "#%<>[\]^`{|}"
CharacterSet.urlPasswordAllowed // URL中密码子模块中允许的字符集. "#%/:<>?@[\]^`{|}"
TO
iOS CharacterSet(字符集)简单理解
https://blog.csdn.net/ZY_FlyWay/article/details/88795367
IOS 文本操作笔记——CHARACTERSET
https://blog.csdn.net/xiaobo0134/article/details/111885612
这篇关于[Swift]URL编码|CharacterSet字符集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!