Jacksonkr
Jacksonkr

Reputation: 32247

ios NSError types

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?

Upvotes: 5

Views: 2633

Answers (2)

Amngidaan
Amngidaan

Reputation: 21

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

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

Upvotes: 2

mattjgalloway
mattjgalloway

Reputation: 34912

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];
        ...
    });
}];
...

Upvotes: 11

Related Questions