title: iPhone - accessing an NSArray in a ViewController from the AppDelegate?
body: <p>I want to be able to access an array of objects in my iPhone application. The array of objects is populated in the appDelegate of my application and I want to be able to access the array in one of my View Controllers.</p>
  
  <p>I currently set up the array in my appDelegate.h file as follows:</p>
  
  <pre><code>NSArray *listObjArray;
  @property (nonatomic, retain) NSArray *listObjArray;
  </code></pre>
  
  <p>I then populate it with some Strings like this in the AppDelegate:</p>
  
  <pre><code>listObjArray = [NSArray arrayWithObjects:@"Hello", @"How", @"are", nil];
  NSLog(@"Array size = %i", [listObjArray count]);
  </code></pre>
  
  <p>It is synthesized and also released in dealloc. The NSLog returns the correct count here.</p>
  
  <p>In my ViewController class I import the appDelegate like this:</p>
  
  <pre><code>#import "MyaAppDelegate.h"
  </code></pre>
  
  <p>I then access my appDelegate and the NSArray like this and try to Log the count in my View Controller:</p>
  
  <pre><code>MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] 
                                         delegate];
  
  NSLog(@"Before array set");
  NSArray *newArray = [appDelegate listObjArray];
  NSLog(@"After array set");
  NSLog(@"array count = %i", [newArray count]);
  NSLog(@"After array count");
  </code></pre>
  
  <p>The logging here gets to "After array set" and then I get "EXC_BAD_ACCESS" on the line where I try to print the count from the array in the View Controller.</p>
  
  <p>The printing of the count works fine from the appDelegate and setting the newArray as the array from the delegate appears to work yet I cant do anything with it then.</p>
  
  <p>Can anyone see what I am doing wrong?</p>