【问题标题】:ios NSError typesios NSError 类型
【发布时间】:2012-02-08 23:15:03
【问题描述】:

自从我添加了这个 async 请求后,我收到了一个 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,那么我的代码运行良好,但我对不使用错误感到不安。我该怎么办?

【问题讨论】:

  • 如果你实际上没有对错误做任何事情,你也可以只传递 NULL。您通常可以检测到是否发生了错误,因为该方法返回 nil,因此您不会错过发生错误的事实。

标签: ios


【解决方案1】:

大概是因为您在完成处理程序中重用了传递给您的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];
        ...
    });
}];
...

【讨论】:

    【解决方案2】:

    当将NSError *error=nil; 定义放在 ^block 之外时会发生此Xcode 错误。

    在块内,error:&error 工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 2018-11-05
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多