ios - ARC : returning allocated object from a method called by performSelector, 可以吗?

标签 ios objective-c automatic-ref-counting performselector

我不确定我是否在这里造成泄漏,是否可以将分配的 NSError 返回给 执行选择器的调用方法? 创建 NSMutableArray 并将其存储在我为回调获得的同一对象中是否可以?然后将其传递给代表? 代码工作正常,但因为我是 arc 的新手,所以我害怕做错什么。

(我正在使用执行选择器,因为我的选择器是动态的。只是为了示例,我是静态编写的)。

AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request 
   success:^(AFHTTPRequestOperation *operation, id responseObject) {

        //-----------------Callback--------------------

        #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        SEL callback = @selector(getOperationCallback:);
        NSError *error = [self performSelector:callback withObject:operation];

        //------------------Delegate Call---------------
        if(operation.delegate)
            [operation.delegate onFinish:operation.requestIdentifier error:error 
                                                    data:operation.parsedObject];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        //------------------Delegate Call---------------
        if(operation.delegate)
            [operation.delegate onFinish:operation.requestIdentifier error:error data:nil];

    }];

- (NSError *)getOperationCallback:(AFHTTPRequestOperation *)operation{

    NSArray *rawJson = (NSArray *)operation.jsonObject;
    NSError *error;

    NSMutableArray *array = [[NSMutableArray alloc] init];

    for(id json in rawJson){
        MyObject *object = [[MyObject alloc] initWithJson:json];
        if(object){
            [array addObject:object];
        }else{
            error = [NSError errorWithDomain:@"myErrors" code:1000 userInfo:nil];
            break;
        }
    }

    operation.parsedObject = array;

    return error;
}

最佳答案

通常,如果您传递给它的选择器以 allocnewretaincopymutableCopy

关于ios - ARC : returning allocated object from a method called by performSelector, 可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14545814/

相关文章:

ios - 更改没有淡入淡出效果的 UIButton 标题?

ios - UITableView:如何通过长按更改自定义单元格中的 UIView 状态?

objective-c - Objective-C : @property declaration and instance variable declaration

swift - 为什么调用 deinit 方法后可以访问对象?

javascript - 移动设备的屏幕宽度(android和ipod)

ios - 如何在swift中以菱形显示图像?

iphone - 在第二个 View 中加载选项卡栏

iphone - 单击 "back"按钮时 UINavigationController 应用程序崩溃

带有 ARC : Custom setter does not retain 的 Objective-C

ios - 快速函数调用中是否有必需的参数顺序