I have a NSDate object
NSDate* time = [NSDate date];
I'm trying to save it using NSKeyedArchiver using the following
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveFile = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"GameData.dat"];
NSMutableData *gameData = [NSMutableData data];
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData: gameData];
[encoder encodeObject: time forKey: @"time"];
[encoder finishEncoding];
[gameData writeToFile:saveFile atomically:YES];
And I load it using the following
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"GameData.dat"];
Boolean saveFileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
NSMutableData *gameData = [NSData dataWithContentsOfFile:file];
NSKeyedUnarchiver *decoder [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];
time = [decoder decodeObjectForKey: @"time"];
This method works fine for all non object data types but it always returns nil for objects. I read the apple documentation for NSKeyedArchiver and it looks like I should be doing it right but as always I'm probably missing something obvious. Any help would be appreciated.