目标-C';CFURLCreateStringByAddingPercentEscapes';已弃用:首次在iOS 9.0中弃用

目标-C';CFURLCreateStringByAddingPercentEscapes';已弃用:首次在iOS 9.0中弃用,ios,objective-c,Ios,Objective C,我得到这个警告: 'CFURLCreateStringByAddingPercentEscapes' is deprecated: first deprecated in iOS 9.0 - Use [NSString stringByAddingPercentEncodingWithAllowedCharacters:] instead, which always uses the recommended UTF-8 encoding, and which encodes for a spec

我得到这个警告:

'CFURLCreateStringByAddingPercentEscapes' is deprecated: first deprecated in iOS 9.0 - Use [NSString stringByAddingPercentEncodingWithAllowedCharacters:] instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent (since each URL component or subcomponent has different rules for what characters are valid).
在这一行:

return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding));
我试图用谷歌搜索一个解决方案,但我什么都不懂。请帮忙

我尝试了以下方法:

return [NSString stringByAddingPercentEncodingWithAllowedCharacters:(NULL, (CFStringRef)self, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding))];
得到了新的警告:

Class method '+stringByAddingPercentEncodingWithAllowedCharacters:' not found (return type defaults to 'id')

好的,我来荡秋千:

- (NSString *)escaped
{
#if (TARGET_OS_MAC && (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9))
    NSString * encodedString = (NSString *)
    CFURLCreateStringByReplacingPercentEscapes(
                                               NULL,
                                               (CFStringRef)self,
                                               NULL);
#else
    NSString * encodedString = (NSString *)
    CFURLCreateStringByAddingPercentEscapes(
        NULL,
        (CFStringRef)self,
        NULL,
        (CFStringRef)@"!*'();:@&=+$,/?%#[]",
        kCFStringEncodingUTF8 );
#endif
    return encodedString;
}

使用
[NSString stringByAddingPercentEncodingWithAllowedCharacters:][/code>代替。对不起,我不想听起来粗鲁,但你写过一行Objective-C吗?我确实觉得这很粗鲁。我有,但我不擅长代码转换。请查看
string的文档,添加允许字符的百分比编码:
。它不是类方法,只接受一个参数,即
NSCharacterSet
- (NSString *)escaped
{
#if (TARGET_OS_MAC && (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9))
    NSString * encodedString = (NSString *)
    CFURLCreateStringByReplacingPercentEscapes(
                                               NULL,
                                               (CFStringRef)self,
                                               NULL);
#else
    NSString * encodedString = (NSString *)
    CFURLCreateStringByAddingPercentEscapes(
        NULL,
        (CFStringRef)self,
        NULL,
        (CFStringRef)@"!*'();:@&=+$,/?%#[]",
        kCFStringEncodingUTF8 );
#endif
    return encodedString;
}