Why does NSError need double indirection? (pointer to a pointer)

前端 未结 5 1437
遇见更好的自我
遇见更好的自我 2020-11-30 18:53

This concept seems to trouble me. Why does an NSError object need its pointer passed to a method that is modifying the object? For instance, wouldn\'t just passing a referen

5条回答
  •  不知归路
    2020-11-30 19:13

    The NSError** pattern is used when a method normally returns some value but instead may need to return an error object (of type NSError*) if it fails. In Objective-C a method can only return one type of object, but this is a case where you want to return two. In C-like languages when you need to return an extra value you ask for a pointer to a value of that type, so to return an NSError* you need an NSError** parameter. A more realistic example would be this:

    // The method should return something, because otherwise it could just return
    // NSError* directly and the error argument wouldn't be necessary
    - (NSArray *)doStuffWithObject:(id)obj error:(NSError **)error
    {
      NSArray *result = ...;  // Do some work that might fail
      if (result != nil) {
        return result;
      } else {
        // Something went bad!
        // The caller might pass NULL for `error` if they don't care about
        // the result, so check for NULL before dereferencing it
        if (error != NULL) {
          *error = [NSError errorWithDomain:...];
        }
        return nil;  // The caller knows to check error if I return nil
      }
    }
    

    If you only had an NSError* parameter instead of an NSError** then doStuff would never be able to pass the error object back to its caller.

提交回复
热议问题