ios NSError类型

Jac*_*nkr 5 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];
        ...
    });
}];
...
Run Code Online (Sandbox Code Playgroud)

如果我使用error:nil那么我的代码运行正常,但我对不使用错误感到不安.我该怎么办?

mat*_*way 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];
        ...
    });
}];
...
Run Code Online (Sandbox Code Playgroud)