title: How to encode and decode a custom class with NSKeyedArchiver
body: <p>I have a custom class that I wish to save and load. The class contains an NSDate, an NSString, and an NSNumber. I have implemented the NSCoding protocol in the .h file. Here is the code I have so far. theDate is an NSDate. theName is the NSString. homeAway is the NSNumber.</p>
  
  <pre><code>-(void)encodeWithCoder:(NSCoder *)aCoder {
  [aCoder encodeObject:theDate forKey:@"theDate"];
  [aCoder encodeObject:theName forKey:@"theName"];
  [aCoder encodeObject:homeAway forKey:@"homeAway"];
  }
  
  -(id)initWithCoder:(NSCoder *)aDecoder {
  if ((self = [super init])) {
      theDate = [aDecoder decodeObjectForKey:@"theDate"];
      theName = [aDecoder decodeObjectForKey:@"theName"];
      homeAway = [aDecoder decodeObjectForKey:@"homeAway"];
  }
  return self;
  }
  </code></pre>
  
  <p>I am using the code below to load my custom object. When I use print-object in the Debugger only the homeAway shows up as an actual object. theDate and theName say 0x4e4f150 does not appear to point to a valid object.</p>
  
  <pre><code>[gamesArray setArray:[NSKeyedUnarchiver unarchiveObjectWithFile:path]];  
  Game *loadedGame = [gamesArray objectAtIndex:gameNumber];
  </code></pre>
  
  <p>I save the data using the following code:</p>
  
  <p>I use this to call my class to create a new Game (the custom class that I am trying to save). The code up to NSDate *aDate = [gameDate date]; is irrelevant.</p>
  
  <pre><code>-(void)newGame {
  if (homeAway != 0) {
      if (dateChanged == 1) {
          if ([nameField.text length] &gt; 0) {
              [homeButton setImage:[UIImage imageNamed:@"HomeGray.png"] forState:UIControlStateNormal];
              [awayButton setImage:[UIImage imageNamed:@"AwayGray.png"] forState:UIControlStateNormal];
              [dateButton setImage:[UIImage imageNamed:@"DateGray.png"] forState:UIControlStateNormal];
              [nameField setBackground:[UIImage imageNamed:@"textField.png"]];
              NSDate *aDate = [gameDate date];
              NSString *aString = [[[NSString alloc] initWithString:[nameField text]] autorelease];
              UIApplication *app = [UIApplication sharedApplication];
              [[[(miShotTrackAppDelegate *)[app delegate] viewController] courtViewOne] newGame:aDate withName:aString andPlace:homeAway];
              [loadTable reloadData];
              datePicke = 0;
              homeAway = 0;
              dateChanged = 0;
              nameField.text = @"";
              [self goBack];
              [self autoSave];
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>From that method I call the newGame method which is</p>
  
  <pre><code>-(void)newGame:(NSDate *)theDate withName:(NSString *)theName andPlace:(int)homeAway {
  
  Game *newGame = [[Game alloc] init];
  NSDate *tDate = [NSDate new];
  tDate = theDate;
  [newGame setTheDate:tDate];
  NSString *tString = [NSString stringWithString:theName];
  [newGame setTheName:tString];
  NSNumber *theNumber = [NSNumber numberWithInt:homeAway];
  [newGame setHomeAway:theNumber];
  [gamesArray addObject:newGame];
  gameNumber = [gamesArray count] - 1;
  NSString *path = [self findGamesPath];
  [NSKeyedArchiver archiveRootObject:gamesArray toFile:path];
  [newGame release];
  }
  </code></pre>
  
  <p>Why will my objects not save? Does copying my objects have something to do with it? I do get an error on the commented line that says "exc bad access". Please help...</p>