title: Setting NSError within a block, using ARC
body: <p>I wish to set an NSError pointer from within a block in a project using
  automatic reference counting. What follows is a simplified version of my code:</p>
  
  <pre><code>- (BOOL)frobnicateReturningError:(NSError **)error
  {
      NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
  
      __block Frobnicator *blockSelf = self;
      [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
          [blockSelf doSomethingWithItem:item error:error];
      }];
  }
  </code></pre>
  
  <p>This compiles but given <code>error</code> may be modified by
  <code>doSomethingWithItem</code> I tried creating a local NSError for the block to
  modify, which would then be used to set the original <code>error</code> after
  the enumeration (which I haven't shown):</p>
  
  <pre><code>- (BOOL)frobnicateReturningError:(NSError **)error
  {
      NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
  
      __block Frobnicator *blockSelf = self;
      __block NSError *blockError = nil;
      [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
          [blockSelf doSomethingWithItem:item error:&amp;blockError];
      }];
  }
  </code></pre>
  
  <p>This fails to compile with the following error:</p>
  
  <blockquote>
    <p>passing address of non-local object to __autoreleasing parameter for write-back</p>
  </blockquote>
  
  <p>Googling for this error only returns results from the Clang source code itself.</p>
  
  <p>One solution that seems to work but is a bit ugly is to have an inner and outer error pointer:</p>
  
  <pre><code>- (BOOL)frobnicateReturningError:(NSError **)error
  {
      NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
  
      __block Frobnicator *blockSelf = self;
      __block NSError *outerError = nil;
      [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
          NSError *innerError = nil;
          [blockSelf doSomethingWithItem:item error:&amp;innerError];
          outerError = innerError;
      }];
  }
  </code></pre>
  
  <p>What is the correct way to set an NSError from within a block?</p>