title: Storing custom objects in an NSMutableArray in NSUserDefaults
body: <p>I've recently been trying to store the search results of my iPhone app in the NSUserDefaults collection. I also use this to save user registration info successfully, but for some reason trying to store my NSMutableArray of custom Location classes always comes back empty.</p>
  
  <p>I tried converting the NSMutableArray to a NSData element as of this post but I get the same result (<a href="https://stackoverflow.com/questions/350848/possible-to-save-an-integer-array-using-nsuserdefaults-on-iphone">Possible to save an integer array using NSUserDefaults on iPhone?</a>)</p>
  
  <p>The code samples I have tried are:</p>
  
  <p><strong>Save:</strong> </p>
  
  <pre><code>[prefs setObject:results forKey:@"lastResults"];
  [prefs synchronize];
  </code></pre>
  
  <p>or </p>
  
  <pre><code>NSData *data = [NSData dataWithBytes:&amp;results length:sizeof(results)];
  [prefs setObject:data forKey:@"lastResults"];
  </code></pre>
  
  <p>or</p>
  
  <pre><code>NSData *data = [NSKeyedArchiver archivedDataWithRootObject:results];
  [prefs setObject:data forKey:@"lastResults"];
  </code></pre>
  
  <p><strong>Load:</strong> </p>
  
  <pre><code>lastResults = (NSMutableArray *)[prefs objectForKey:@"lastResults"];
  </code></pre>
  
  <p>or</p>
  
  <pre><code>NSData *data = [prefs objectForKey:@"lastResults"];
  memcpy(&amp;lastResults, data.bytes, data.length);  
  </code></pre>
  
  <p>or</p>
  
  <pre><code>NSData *data = [prefs objectForKey:@"lastResults"];
  lastResults = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  </code></pre>
  
  <p>After following advice below I have also implemented NSCoder in my object (ignore the overuse of NSString its temporary):</p>
  
  <pre><code>#import "Location.h"
  
  
  @implementation Location
  
  @synthesize locationId;
  @synthesize companyName;
  @synthesize addressLine1;
  @synthesize addressLine2;
  @synthesize city;
  @synthesize postcode;
  @synthesize telephoneNumber;
  @synthesize description;
  @synthesize rating;
  @synthesize priceGuide;
  @synthesize latitude;
  @synthesize longitude;
  @synthesize userLatitude;
  @synthesize userLongitude;
  @synthesize searchType;
  @synthesize searchId;
  @synthesize distance;
  @synthesize applicationProviderId;
  @synthesize contentProviderId;
  
  - (id) initWithCoder: (NSCoder *)coder
  {
      if (self = [super init])
      {
          self.locationId = [coder decodeObjectForKey:@"locationId"];
          self.companyName = [coder decodeObjectForKey:@"companyName"];
          self.addressLine1 = [coder decodeObjectForKey:@"addressLine1"];
          self.addressLine2 = [coder decodeObjectForKey:@"addressLine2"];
          self.city = [coder decodeObjectForKey:@"city"];
          self.postcode = [coder decodeObjectForKey:@"postcode"];
          self.telephoneNumber = [coder decodeObjectForKey:@"telephoneNumber"];
          self.description = [coder decodeObjectForKey:@"description"];
          self.rating = [coder decodeObjectForKey:@"rating"];
          self.priceGuide = [coder decodeObjectForKey:@"priceGuide"];
          self.latitude = [coder decodeObjectForKey:@"latitude"];
          self.longitude = [coder decodeObjectForKey:@"longitude"];
          self.userLatitude = [coder decodeObjectForKey:@"userLatitude"];
          self.userLongitude = [coder decodeObjectForKey:@"userLongitude"];
          self.searchType = [coder decodeObjectForKey:@"searchType"];
          self.searchId = [coder decodeObjectForKey:@"searchId"];
          self.distance = [coder decodeObjectForKey:@"distance"];
          self.applicationProviderId = [coder decodeObjectForKey:@"applicationProviderId"];
          self.contentProviderId = [coder decodeObjectForKey:@"contentProviderId"];
      }
  }
  
  - (void) encodeWithCoder: (NSCoder *)coder
  {
      [coder encodeObject:locationId forKey:@"locationId"];
      [coder encodeObject:companyName forKey:@"companyName"];
      [coder encodeObject:addressLine1 forKey:@"addressLine1"];
      [coder encodeObject:addressLine2 forKey:@"addressLine2"];
      [coder encodeObject:city forKey:@"city"];
      [coder encodeObject:postcode forKey:@"postcode"];
      [coder encodeObject:telephoneNumber forKey:@"telephoneNumber"];
      [coder encodeObject:description forKey:@"description"];
      [coder encodeObject:rating forKey:@"rating"];
      [coder encodeObject:priceGuide forKey:@"priceGuide"];
      [coder encodeObject:latitude forKey:@"latitude"];
      [coder encodeObject:longitude forKey:@"longitude"];
      [coder encodeObject:userLatitude forKey:@"userLatitude"];
      [coder encodeObject:userLongitude forKey:@"userLongitude"];
      [coder encodeObject:searchType forKey:@"searchType"];
      [coder encodeObject:searchId forKey:@"searchId"];
      [coder encodeObject:distance forKey:@"distance"];
      [coder encodeObject:applicationProviderId forKey:@"applicationProviderId"];
      [coder encodeObject:contentProviderId forKey:@"contentProviderId"];
  
  }
  </code></pre>