转换为ARC时NSMakeCollectable出现问题

转换为ARC时NSMakeCollectable出现问题

本文介绍了转换为ARC时NSMakeCollectable出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的代码转换为ARC,而我在ASIRequest库中遇到NSMakeCollectable问题。

I am trying to convert my code to ARC and I have problem with NSMakeCollectable in the ASIRequest library.

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

它给了我这个错误: NSMakeCollectable不可用:自动引用计数不可用

如何解决此问题?需要一些指导。谢谢。

How do I solve this? Need some guidance. Thanks.

编辑:
试过这个但没有工作:

Tried this and not working:

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


推荐答案

NSMakeCollectable ARC中不再需要。您可以在此使用 CFBridgingRelease

NSMakeCollectable is no longer required in ARC. You can use CFBridgingRelease here.

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

这篇关于转换为ARC时NSMakeCollectable出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:13