title: Strong References (Objective -C)
body: <p>I'm trying to better understand Strong References, but in the context of a particular example that I will list below.</p>
  
  <p>Lets say that I have a global variable called gPrefs.  It's of type Preference Class, a custom class I made.</p>
  
  <p>I've declared in its interface the following...</p>
  
  <pre><code>@property (strong) NSURL            *masterFolder;
  </code></pre>
  
  <p>This is a pointer to a NSURL Object.  I have given it (strong) reference meaning it will stick around and not release it's memory.</p>
  
  <p>Now lets say I have an instance function somewhere else entirely that looks like the following below.</p>
  
  <pre><code>- (IBAction)masterDataSelectButtonAction:(id)sender
  {
      NSOpenPanel *openPanel = [NSOpenPanel openPanel];//creates an open panel.
      [openPanel setCanChooseDirectories:YES];
      [openPanel setCanChooseFiles:NO];
  
      NSInteger panelResult = [openPanel runModal];
  
      if(panelResult == NSFileHandlingPanelCancelButton){
          NSLog(@"masterDataSelectButtonAction -&gt; Cancel Button Pressed");
          return;
      }
  
      if(panelResult == NSFileHandlingPanelOKButton){
          NSLog(@"masterDataSelectButtonAction-&gt; Ok Button Pressed");
          NSURL *theUrl = [openPanel URL];
          gPrefs.masterFolder = theUrl; //Setting (strong ref) point to local.
          [_masterDataLabel setStringValue:[gPrefs.masterFolder absoluteString]];
      }
  }
  </code></pre>
  
  <p>If the person pressed the OK button of the panel, I create a local pointer instance variable called theUrl.  I then set the global variables masterFolder pointer to the local instance variables pointer.</p>
  
  <p>If I understand correctly ARC is supposed to release objects at the end of functions when it deems it's not being used anymore.</p>
  
  <p>Therefore theUrl should be released at the end of the function.</p>
  
  <p>However, if I understand (strong) reference, the compiler perhaps is smart enough to realize that since I changed the global preferences pointer to a different pointer it will keep the other pointers reference in memory alive (or retain it).</p>
  
  <p>If I had not declared my masterFolder pointer as (strong) would the masterFolder pointer be nil after the function ended?</p>
  
  <p>My experiments show that keeping it (strong) seems to retain the value from the localInstance pointer despite reaching the end of the local instance function.</p>
  
  <p>My question is why? Even if masterFolder is a strong reference, if I assigned it to a localInstance pointer and that pointer is removed at the end of the Instance function, why did the value of masterFolder not NIL out when I read from it again outside the instance function anywhere else.</p>
  
  <p>My understanding of (strong) references is a bit (weak).  No pun intended.</p>