简体   繁体   English

STTwitter Library中不推荐使用'CFURLCreateStringByAddingPercentEscapes'

[英]'CFURLCreateStringByAddingPercentEscapes' deprecated in STTwitter Library

I'm using the STTwitter Library in my App and it's been throwing a deprecation warning that says the following: 我正在我的应用程序中使用STTwitter库,并且它已经抛出了一个弃用警告,其中说明了以下内容:

 '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).' deprecated
error. 

Here's the code that is causing the problem in STTwitter: 这是导致STTwitter问题的代码:

@implementation NSString (RFC3986)
- (NSString *)st_stringByAddingRFC3986PercentEscapesUsingEncoding:(NSStringEncoding)encoding {

    NSString *s = (__bridge_transfer NSString *)(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                         (CFStringRef)self,
                                                                                         NULL,
                                                                                         CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                                         kCFStringEncodingUTF8));
    return s;
}
@end

The question is how to replace this with equivalent code using 'stringByAddingPercentEncodingWithAllowedCharacter'. 问题是如何使用'stringByAddingPercentEncodingWithAllowedCharacter'将其替换为等效代码。

Here's valid replacement code: 这是有效的替代代码:

@implementation NSString (RFC3986)
- (NSString *) st_stringByAddingRFC3986PercentEscapesUsingEncoding: (NSStringEncoding) encoding {

   NSMutableCharacterSet *allowed = [NSMutableCharacterSet alphanumericCharacterSet];
   [allowed addCharactersInString: @"-._~"];

   return( [self stringByAddingPercentEncodingWithAllowedCharacters: allowed] );
   }    
@end

I've tested the new code verses the old and in all cases, thus far, the strings they generate are equal. 我已经测试了新代码和旧代码,并且在所有情况下,到目前为止,它们生成的字符串是相同的。

In the original code, the idea was to escape a specific named set of characters. 在原始代码中,想法是转义特定的命名字符集。 They were: 他们是:

!*'();:@&=+$,/?%#[]

When you switch to using 'stringByAddingPercentEncodingWithAllowedCharacters', the logic reverses and only characters not named are escaped. 切换到使用'stringByAddingPercentEncodingWithAllowedCharacters'时,逻辑会反转,只有未命名的字符才会被转义。 So, the new code specifies 'alphanumericCharacterSet' and a small group of additional characters are added to it. 因此,新代码指定了'alphanumericCharacterSet',并添加了一小组附加字符。 The added characters are: 添加的字符是:

-._~

Those characters not named in the new code are the same set set of characters explicitly named in the old code.. 在新代码中未命名的那些字符是旧代码中明确命名的相同集合字符集。

As an aside, it is not clear to me that that the passed encoding parameter in the original code was ever used. 顺便说一句,我不清楚原始代码中传递的编码参数是否曾被使用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM