当前位置:网站首页>URL 返回nil 以及urlhash处理
URL 返回nil 以及urlhash处理
2022-07-27 14:55:00 【CAir2】
在项目实践中,发现URL在某些系统版本(14.7.1)会返回nil(即使不存在中文),所以在使用URL的时候最好进行以下编码:
在了解这个之前我们应该先了解一下url中#(hash)的含义
Swift3之前:
url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
Swift3:
//编码
url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
//解码
url.removingPercentEncoding
对String进行扩展:
extension String{
public func toURL()->URL?{
//如果url带有hash则不处理
let range = range(of: "/#/")
if range != nil{
let Url = URL(string: self)
return Url
}
let eurl = addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if eurl != nil{
let Url = URL(string: eurl!)
return Url
}
return nil
}
}
边栏推荐
- mvc和mvp和mvvm的区别
- Flex弹性盒布局2
- jsp-El表达式,JSTL标签
- Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
- Cvxpy - latest issue
- Rotate the whole model in ADAMS
- Database notes sorting
- JSON data parsing
- Niuke topic -- judge whether it is a complete binary tree or a balanced binary tree
- JD Zhang Zheng: practice and exploration of content understanding in advertising scenes
猜你喜欢
随机推荐
Natural sorting: comparable interface, customized sorting: the difference between comparator interface
C语言之动态内存分配
被动收入:回归原始且安全的两种赚取方法
Rotate the whole model in ADAMS
UML diagram introduction
Mpc5744p clock module
CDQ divide and conquer and whole dichotomy learning notes
Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
Random number formula random
为媒体资产构建一个云原生的文件系统
Shardingsphere-proxy-5.0.0 distributed snowflake ID generation (III)
[paper reading] single- and cross modality near duplicate image pairsdetection via spatial transformer compare
jsp-El表达式,JSTL标签
log4j.jar和slf4-log4下载链接
Jerry's book can't find Bluetooth solutions [article]
Servlet中文乱码setContentType设置无效
The difference between MVC and MVP and MVVM
[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images
Passive income: return to the original and safe two ways to earn
了解Bom与DOM的基本属性








