自iOS9以后字符串的解码方法 - (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc 就被
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters方法给代替了。
具体的实现的方法如下:
//定义一个字符串
NSString *str = @"http://apis.haoservice.com/weather?cityname=北京&key=c056e50a2bde4478a1a31ae491799607";
//把字符串转换为NSCharacterSet类型
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:str];
// 再根据NSCharacterSet类型转换字符串的中文为相应的编码,
NSString *encodedValue = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
NSLog(@"%@",encodedValue);
运行结果如下:
encodedValue = http://apis.haoservice.com/weather?cityname=%E5%8C%97%E4%BA%AC&key=c056e50a2bde4478a1a31ae491799607。