Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with NSMakeCollectable when converting to ARC

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 @"";
        }
like image 391
lakshmen Avatar asked Mar 04 '13 08:03

lakshmen


1 Answers

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 @"";
}
like image 112
iDev Avatar answered Oct 06 '22 01:10

iDev