How would you refactor this kind of code? I've unsuccessfully tried several approaches (NSArray, NSPointerArray, plain C array), but always ran into pointer type checking error.NSError *error1;NSError *error2;NSError *error3;//imagine MANY of them[object1 methodWithError: &error1];[object2 methodWithError: &error2];[object3 methodWithError: &error3];BOOL wasErrorOfCode1 = ([error1 code] == 1) || ([error2 code] == 1) || ([error3 code] == 1);if (wasErrorOfCode1) {//do stuff
Storing and iterating over multiple NSError pointers
objective c;error handling;cocoa
I want to propose a differently structured code, that especially will be easier to adapt for more objects. NSArray *objects = @[object1, object2, object3];NSMutableArray *errors = [NSMutableArray array];[objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {    NSError *error;    [obj methodWithError: &error];    if(error){        [errors addObject: @{@error:error,@object:obj}];    }}];Now you can iterate over all errors and examine them along with the object the error occurred on. [errors enumerateObjectsUsingBlock:^(NSDictionary *errorDict, NSUInteger idx, BOOL *stop) {    NSError *error = errorDict[@error];    id object = errorDict[@object];    if(){        //add an appropriate test    }}];you can't add the errors into an array upfront, as NSError *error1;will result into a nil-object for the variable error1. NS(Mutable)Arrays can't handle nil objects. Same for dictionaries.On the other hand my solution allows adding the errors, as it only add them once an existing objects is assigned to the variable error.