iOS9之后对于NSURL的编码转换方法变化说明

在iOS9之后,官方推荐使用下面的方法对NSString进行转换

- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters NS_AVAILABLE(10_9, 7_0);

  • 举个例子:

使用JSON获取http://itunes.apple.com/search?term=%@ 的数据时,若%@中存在需要编码的字符(如空格)可使用上面的方法对%@转换,其中allowedCharacters变量设置为:[NSCharacterSet URLPathAllowedCharacterSet]

若%@为“Jack Ma”,则转换后的URL为http://itunes.apple.com/search?term=Jack%20Ma

原文地址:https://www.cnblogs.com/jackma86/p/4982261.html