stringByAddingPercentEscapesUsingEncoding已弃用

时间:2017-03-01 10:41:01

标签: objective-c

这是不推荐使用的“stringByAddingPercentEscapesUsingEncoding”,我想用更新一行替换这一行。

NSString *encodedString = [modalData.imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

3 个答案:

答案 0 :(得分:3)

尝试以下代码行:

NSString *encodedString = [modalData.imageURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

<强> stringByAddingPercentEncodingWithAllowedCharacters:

  

通过替换all返回从接收器创建的新字符串   不在指定集合中的字符,包含百分比编码字符。

字符集传递给 - stringByAddingPercentEncodingWithAllowedCharacters

+ (NSCharacterSet *)URLUserAllowedCharacterSet;
+ (NSCharacterSet *)URLPasswordAllowedCharacterSet;
+ (NSCharacterSet *)URLHostAllowedCharacterSet;
+ (NSCharacterSet *)URLPathAllowedCharacterSet;
+ (NSCharacterSet *)URLQueryAllowedCharacterSet;
+ (NSCharacterSet *)URLFragmentAllowedCharacterSet;

参考:https://developer.apple.com/reference/foundation/nsstring/1411946-stringbyaddingpercentencodingwit

答案 1 :(得分:0)

使用此代码

NSString *encodedString = [modalData.imageURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

答案 2 :(得分:0)

此代码对我有用:

NSString *url = [modalData.imageURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];