title: Idiomatic short lifespan local objects akin to RAII
body: <p>I came across this fragment of Objective-C:</p>
  
  <pre><code>NSNumber *theBalance = 
      [[[NSNumberFormatter alloc] init]
       numberFromString: [textField text]];
  </code></pre>
  
  <p>This seems to leak the <code>NSNumberFormatter</code>. In C++ I would do one of two things:</p>
  
  <ol>
  <li>use <code>auto</code> (i.e. stack) storage for the <code>NSNumberFormatter</code></li>
  <li>use RAII (e.g. <code>shared_ptr</code>)  to manage the life of the <code>NSNumberFormatter</code></li>
  </ol>
  
  <p>In Objective-C neither of these options seem to be possible. I tried on the stack:</p>
  
  <pre><code>NSNumberFormatter fmt;
  </code></pre>
  
  <p>But this doesn't compile. As far as I can find there's no direct equivalent of RAII in Objective-C. I'm probably looking at the problem from the wrong angle as a mainly C++ programmer, so:</p>
  
  <ol>
  <li>In the general case what's the correct, idiomatic (modern) Objective-C way of handling the life of objects like the <code>NSNumberFormatter</code> here? Do I really have to do it explicitly myself?</li>
  <li>In the specific case is there a better way of solving the actual problem?</li>
  </ol>