获取强烈捕获对象的ARC警告

时间:2011-11-23 00:13:52

标签: iphone objective-c cocoa-touch memory-management automatic-ref-counting

我正在使用ARC并收到警告Capturing 'request' strongly in this block is likely to a retain cycle.

__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setCompletionBlock:^{
        NSString *responseString = [request responseString];
        self.appointmentArray = [responseString JSONValue];
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"%@", error.description);
    }];

2 个答案:

答案 0 :(得分:9)

我假设request在块之前的某处被声明。您需要将其声明为__weak,或者为其设置第二个弱声明变量。

This question类似。试试这个:

__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
__weak ASIHTTPRequest *request_b = request;
[request setCompletionBlock:^{
    NSString *responseString = [request_b responseString];
    self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
    NSError *error = [request_b error];
    NSLog(@"%@", error.description);
}];

答案 1 :(得分:6)

简单地替换:
__ block ASIFormDataRequest * request = [[ASIFormDataRequest alloc] initWithURL:url];

搭配:
__弱 ASIFormDataRequest * request = [[ASIFormDataRequest alloc] initWithURL:url];

就够了。