Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios NSError types

Tags:

ios

Ever since I added this async request, I'm getting an xcode error of Sending 'NSError *const __strong *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer

...
[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:data error:&error];
        ...
    });
}];
...

If I use error:nil then my code runs fine, but I feel uneasy about not using errors.. What should I do?

like image 473
Jacksonkr Avatar asked Feb 08 '12 23:02

Jacksonkr


2 Answers

Presumably it's because you're reusing the error passed in to you in the completion handler. It'll be being passed as __strong and then you pass it in where it is required to be __autoreleasing. Try changing to this code:

...
[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
    dispatch_async(dispatch_get_main_queue(), ^{
        NSError *error2 = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:data error:&error2];
        ...
    });
}];
...
like image 55
mattjgalloway Avatar answered Nov 07 '22 16:11

mattjgalloway


This Xcode error happens when put NSError *error=nil; definition outside the ^block.

Inside the block, then error:&error works fine.

like image 2
Amngidaan Avatar answered Nov 07 '22 16:11

Amngidaan