Objective-C' CFURLCreateStringByAddingPercentEscapes'不推荐使用:首先在iOS 9.0中弃用

时间:2015-10-02 13:05:10

标签: 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 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')

Expression result unused

1 个答案:

答案 0 :(得分:-1)

好的,我会摇摆不定:

- (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;
}