title: iPhone +NSCoding/NSKeyedArchiver
body: <p>I Have a small problem with NSCoding and NSKeyedArchiver when persisting my App Settings class and was hoping someone could spot an issue if it's my code, I am relatively new to Obj-C but have plenty of coding experience in numerous languages so the code seems ok to me but...</p>
  
  <p>I have an instance class to hold my app Settings, the class itself is a retained property of the main App Delegate, this is created via a</p>
  
  <pre><code>@interface AppDelegate : NSObject &lt;UIApplicationDelegate&gt; {
      Settings *settings;
      [...] 
  }
  
  @property (nonatomic, retain) Settings *settings;
  [...]
  </code></pre>
  
  <p>This is created in the applicationDidFinishLaunching event as:</p>
  
  <pre><code>settings = [Settings LoadSettings];
  </code></pre>
  
  <p>If I comment out the above line then the app works fine every time, however if I pull the oject back from persisted settings using NSCoder and NSKeyedUnarchiver, the SIGARBT error is thrown as a NSCFString selector is being sent for what is encoded as a boolean property? The Settings Class is defined as an NSObject which implements the protocol.</p>
  
  <pre><code>@interface Settings : NSObject &lt;NSCoding&gt;
  </code></pre>
  
  <p>As I say, creating an instance of the settings class is fine, there is no issue, saving it seems OK as well as checking the returning class from the LoadSettings method shows the right values, only after exiting the method does the expected bool value seem to be getting sent to the load method as an NSCFString</p>
  
  <pre><code>- (id)initWithCoder:(NSCoder *)decoder {
      if (self = [super init]) {
              [...]
          self.animateMenus = [decoder decodeBoolForKey:@"animateMenus"];
      }
  
      return self;
  }
  
  - (void)encodeWithCoder:(NSCoder *)encoder {
      [...]
      [encoder encodeBool:animateMenus forKey:@"animateMenus"];
  }
  </code></pre>
  
  <p>Once the settings have been loaded the property in question is used like this:</p>
  
  <pre><code>SettingsViewController *settingsView = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil];
  [self presentModalViewController:settingsView animated:[AppDelegate instance].settings.animateMenus];
  [settingsView release];
  </code></pre>
  
  <p>**The animateMenus member of the settings class will now throw the following:</p>
  
  <p>-[NSCFString animateMenus]: unrecognized selector sent to instance 0xc712570 2010-10-15 11:12:51.828 App[900:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString animateMenus]: unrecognized selector sent to instance 0xc712570'</p>
  
  <p>Whereas, taking the 'settings = [Settings LoadSettings];' call out of the app start-up removes the issue (but then always uses the app defaults)?</p>
  
  <p>Load and Save Methods:</p>
  
  <pre><code>+ (Settings*) LoadSettings { 
      Settings *s = nil;
  
      @try {
          NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"settings"];
  
          if (data == nil) {
              s = [[Settings alloc] init];
              [s Initialise];
              [s SaveSettings];
          }
          else
              s = (Settings*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
      }
      @catch (NSException * e) {
          NSLog(@"Error Loading Settings\n%@", [e reason]);
      }
      @finally {
          return s;
      }
  }
  
  // Saves the settings dictionary to the user's device documents folder..
  - (void) SaveSettings { 
      NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
      [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"settings"];
  }
  </code></pre>
  
  <p>Load is a static method all other members of Settings are instance.</p>