zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code

xcodecode to in is object this an
2023-09-11 14:15:06 时间

xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code

解决方法:

方法一:

 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:Api]];
    
    __block ASIHTTPRequest *brRequest = request;
    
    [request setCompletionBlock:^{
        id json = [NSJSONSerialization JSONObjectWithData:[brRequest responseData] options:0 error:Nil];
        //NSLog(@"%@",json);
        NSLog(@"completed");
        
    }];
    
    [request setFailedBlock:^{
        NSLog(@"%@",[brRequest error]);
    }];
    
    [request startAsynchronous];

方法2:

//    //方式二
//    __weak ASIHTTPRequest *werRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:api]];
//    [werRequest setCompletionBlock:^{
//        id json = [NSJSONSerialization JSONObjectWithData:[werRequest responseData] options:0 error:Nil];
//        NSLog(@"%@",json);
//        
//    }];
//    
//    [werRequest setFailedBlock:^{
//        NSLog(@"%@",[werRequest error]);
//    }];
//    
//    [werRequest startAsynchronous];

 

参考:http://stackoverflow.com/questions/7205128/fix-warning-capturing-an-object-strongly-in-this-block-is-likely-to-lead-to-a