How to encode and decode a custom class with NSKeyedArchiver
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.
-(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;
}
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.
[gamesArray setArray:[NSKeyedUnarchiver unarchiveObjectWithFile:path]];
Game *loadedGame = [gamesArray objectAtIndex:gameNumber];
I save the data using the following code:
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.
-(void)newGame开发者_如何学编程 {
if (homeAway != 0) {
if (dateChanged == 1) {
if ([nameField.text length] > 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];
}
}
}
}
From that method I call the newGame method which is
-(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];
}
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...
You're not implementing -initWithCoder correctly. -decodeObjectForKey returns autoreleased object. So the way you wrote the method, you're ending up with dangling pointers for all of your Game ivars. Change -initWithCoder to:
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super init])) {
[self setTheDate:[aDecoder decodeObjectForKey:@"theDate"]];
[self setTheName:[aDecoder decodeObjectForKey:@"theName"]];
[self setHomeAway:[aDecoder decodeObjectForKey:@"homeAway"]];
}
return self;
}
and it should work for you.
精彩评论