首页 文章

ARC禁止将'NSStringEncoding'(又名'unsigned long')隐式转换为'NSCharacterSet * _Nonnull'

提问于
浏览
0

在下面的代码我收到错误消息:ARC禁止将'NSStringEncoding'(又名'unsigned long')隐式转换为'NSCharacterSet * _Nonnull'

我在哪里做错了?

NSURL *candidatesURL = [NSURL URLWithString:[str_url
                                             stringByAddingPercentEncodingWithAllowedCharacters:NSUTF8StringEncoding]];

1 回答

  • 0

    你使用的是错误的方法 . 而不是使用 stringByAddingPercentEncodingWithAllowedCharacters 使用此 stringByAddingPercentEscapesUsingEncoding

    如果您不想使用此方法 stringByAddingPercentEncodingWithAllowedCharacters ,则必须传递一组允许的字符 .

    试试这个 .

    NSURL *candidatesURL = [NSURL URLWithString:[str_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    

    或者,如果您的应用程序开发目标是IOS 9.0及更高版本 .

    不推荐使用 stringByAddingPercentEscapesUsingEncoding ,因此您可以使用 stringByAddingPercentEncodingWithAllowedCharacters 并使用 URLHostAllowedCharacterSet .

    NSURL *candidatesURL = [NSURL URLWithString:[str_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
    

相关问题