在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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-10-25
  • 2022-01-10
  • 2022-12-23
相关资源
相似解决方案