title: Objective-C - When to use 'self'
body: <p>This is unmodified code from Apple's iPhone 'Utility Aplication' template:</p>
  
  <pre><code>- (void)applicationDidFinishLaunching:(UIApplication *)application {
  
   MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
   self.mainViewController = aController;
   [aController release];
  
   mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
   [window addSubview:[mainViewController view]];
   [window makeKeyAndVisible];
  
  }
  </code></pre>
  
  <p>When <code>mainViewController</code> is assigned to <code>aController</code>, the <code>self</code> keyword is specified:</p>
  
  <pre><code> self.mainViewController = aController;
  </code></pre>
  
  <p>However, when the <code>mainViewController</code>'s frame is set, the <code>self</code> keyword is not required:</p>
  
  <pre><code> mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
  </code></pre>
  
  <p>If I remove the <code>self</code> keyword from the first example, the program crashes with the message:</p>
  
  <pre><code>objc[1296]: FREED(id): message view sent to freed object=0x3b122d0
  </code></pre>
  
  <p>If I add the <code>self</code> keyword to the second example, the program runs fine.</p>
  
  <p>Can anyone explain why <code>self</code> is needed in the first case but not the second?  I'm assuming that in both cases <code>mainViewController</code> is referring to the same instance variable.</p>