ios NSError类型

时间:2012-02-08 23:15:04

标签: ios

自从我添加了此异步请求后,我的xcode错误为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];
        ...
    });
}];
...

如果我使用error:nil,那么我的代码运行正常,但我觉得不安关于不使用错误。我该怎么办?

2 个答案:

答案 0 :(得分:11)

大概是因为你在重启处理程序中重用了传递给你的error。它将作为__strong传递,然后您将其传递到需要__autoreleasing的位置。尝试更改为此代码:

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

答案 1 :(得分:2)

error:&error定义置于 ^块后,会出现此Xcode错误。

在块中,{{1}}正常工作。