问题与NSMakeCollectable转换为ARC时(Issue with NSMakeColle

2019-07-20 22:31发布

我想我的代码转换为ARC和我在ASIRequest库有问题,NSMakeCollectable。

- (NSString*)encodeURL:(NSString *)string
{
    NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]);
    if (newString) {
        return newString;
    }
    return @"";
}

它给我这个错误: NSMakeCollectable is unavailable: not available in automatic reference counting

我该如何解决这个问题? 需要一些指导。 谢谢。

编辑:尝试这样做,不工作:

 - (NSString*)encodeURL:(NSString *)string
    {
        NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]);
        if (newString) {
            return (__bridge_transfer NSString*)newString;
        }
        return @"";
    }

试过这还有:

- (NSString*)encodeURL:(NSString *)string
        {
            NSString *newString = NSMakeCollectable([(__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]);
            if (newString) {
                return newString;
            }
            return @"";
        }

Answer 1:

NSMakeCollectable在ARC不再需要。 您可以使用CFBridgingRelease这里。

- (NSString*)encodeURL:(NSString *)string
{
    NSString *newString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])));
    // or  NSString *newString = (__bridge_transfer NSString*)(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding]));
    if (newString) {
        return newString;
    }
    return @"";
}


文章来源: Issue with NSMakeCollectable when converting to ARC