我开始尝试使用ARC,其中一个最早的实验是调用URL并获取一些数据。当然,对我来说HTTP状态码很重要,这意味着我使用了我的“惯用方式”,使用sendSynchronousRequest
,如下所示:
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:responseCode error:error];
启用ARC后,我在最后一行得到了编译错误和警告。错误: 隐式转换 Objective-C 指针为“NSURLResponse *__autoreleasing *”使用 ARC 是不允许的。 隐式转换 Objective-C 指针为“NSError *__autoreleasing *”使用 ARC 是不允许的。 file://localhost/Users/jason/Projects/test/Data/DataService.m: error: Automatic Reference Counting Issue: 隐式转换 Objective-C 指针为“NSURLResponse *__autoreleasing *”使用 ARC 是不允许的。 file://localhost/Users/jason/Projects/test/Data/DataService.m: error: Automatic Reference Counting Issue: 隐式转换 Objective-C 指针为“NSError *__autoreleasing *”使用 ARC 是不允许的。
警告: 不兼容的指针类型发送 'NSHTTPURLResponse *_strong' 给类型为 'NSURLResponse *_autoreleasing *' 的参数。 不兼容的指针类型发送 'NSError *_strong' 给类型为 'NSError *_autoreleasing *' 的参数。
据我所知,引用传递是导致问题的原因,但我不确定解决此问题的正确方法。是否有一种使用ARC实现类似任务的“更好”的方法?