Issue with NSMakeCollectable when converting to AR

2019-02-22 15:50发布

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

It is giving me this error: NSMakeCollectable is unavailable: not available in automatic reference counting.

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

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

Tried this as well:

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

1条回答
SAY GOODBYE
2楼-- · 2019-02-22 16:29

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 @"";
}
查看更多
登录 后发表回答