获取一个强制捕获对象的 ARC 警告

3

我正在使用ARC,并且收到了一个警告,提示在该块中强引用'request'可能会导致循环引用

__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个回答

9
我假设request在块之前已声明,你需要将其声明为__weak,或者设置一个弱声明的第二个变量。

这个问题类似。尝试这样做:

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

现在我声明它的方式是 __block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url]; - user594161
1
在ARC下,__block变量在块内是可写的,但隐式地是__strong类型。 - Alexsander Akers

6

只需要将:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];

替换为:
__weak ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];

就可以了。


注意:本文中的HTML标签已保留。

那只有在你的目标是 iOS 5 的情况下才有效,而现在这可能不是一个好主意。 - Roger Gilbrat
1
在iOS4中,请使用__unsafe_unretained而不是__weak。 - Almog C
+1 为解决我的问题:__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; - PeterK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接