iOS 9.0之后NSString encode方法替换

在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      "#%/:<>?@[\]^`

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

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

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/636486

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。