博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 9.0之后NSString encode方法替换
阅读量:5827 次
发布时间:2019-06-18

本文共 2367 字,大约阅读时间需要 7 分钟。

在iOS 9.0之后,以前常用的NSString编码的方法stringByAddingPercentEscapesUsingEncoding:被弃用了,项目中可能会出现一堆如下️:

'stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.

作为一个有强迫症的优秀程序猿,表示不能忍!

文档里面是这么说的:

- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_DEPRECATED(10_0, 10_11, 2_0, 9_0, "Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.");

很明白,用stringByAddingPercentEncodingWithAllowedCharacters:方法替换之。

这个方法官方文档是这么说的:

// Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.

最后一句Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.,意思就是说,任何非7-bit ASCII字符搁到allowedCharacters里面也将被忽略,也就是allowedCharacters里面的字符跟7-bit ASCII字符不会被编码。

换句话说,上面方法在处理的时候会编码url的中的非7-bit ASCII字符,如这些【`#%^{}"[]|\<>】,如果需要忽略之,需要通过(NSCharacterSet *)allowedCharacters这个参数指定。总结如下

[aString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//等价于[aString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "]];

注意:

  1. 字符集最后是一个空格!
  2. 这里字符集的意思就是,字符串中含有字符集里面的字符将不会被编码。

另外,URL中常用的NSCharacterSet类型定义在分类NSCharacterSet (NSURLUtilities)中,包含字符集如下:

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

以上,还在等什么,全局搜索替换吧,消灭️!

喜欢我的可以关注收藏我的个人博客:

转载地址:http://mqodx.baihongyu.com/

你可能感兴趣的文章
MongoDB CookBook读书笔记之导入导出
查看>>
shell如何快速锁定所有账号
查看>>
HTML 5实现的手机摇一摇
查看>>
Linux 文件IO理解
查看>>
Ninject 2.x细说---2.绑定和作用域
查看>>
30个非常时尚的网页联系表单设计优秀示例
查看>>
使用membership(System.Web.Security)来进行角色与权限管理
查看>>
opticom 语音质量验证白皮书
查看>>
3D实时渲染中的BSP树和多边形剔除
查看>>
Frank Klemm's Dither and Noise Shaping Page: Dither and Noise Shaping In MPC/MP+
查看>>
网络抓包的部署和工具Wireshark【图书节选】
查看>>
Redis在Windows+linux平台下的安装配置
查看>>
Maven入门实战笔记-11节[6]
查看>>
Local declaration of 'content' hides instance variable
查看>>
ASP.NET中 HTML标签总结及使用
查看>>
Linux下日志系统的设计
查看>>
爬虫IP被禁的简单解决方法——切换UserAgent
查看>>
php生成word,并下载
查看>>
紫书 习题8-11 UVa 1615 (区间选点问题)
查看>>
asp.net mvc学习(Vs技巧与Httpcontext)
查看>>