0

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.

Tiddly
  • 1,620
  • 3
  • 21
  • 43
  • 1
    before writing do `assert(gameData.length)` and after reading the data too -- see if the Data is there ok – Daij-Djan Jun 26 '13 at 21:42
  • Side Note - the `fileExistsAtPath:` method has a return type of `BOOL`, not `Boolean`. – rmaddy Jun 26 '13 at 21:47
  • Don't forget to call `finishDecoding` when you are done decoding. – rmaddy Jun 26 '13 at 21:49
  • Weird I commented out all the other variables I was encoding so I could check the gameData.length with just the problem object and it works. For some reason saving it with other variables causes it to break. I'll have to play around with it for a bit to figure out exactly what is breaking it :/ – Tiddly Jun 26 '13 at 22:37
  • Update your question with the code that tries to encode the `NSDate` and other values. Maybe we can help you find the problem. – rmaddy Jun 27 '13 at 00:04

1 Answers1

0

After sending all your objects to the archiver you need to send it a finishEncoding message so that it can complete the data stream.

You also might want to check out the convenience method +archivedDataWithRootObject: or +archiveRootObject:toFile: on NSKeyedArchiver. With this you can replace most of your code with a single line. For the NSKeyedUnarchiver there are analog methods available.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • Sorry I forgot to add the bit after I encode the data to the code snippet. I am calling finishEncoding and any objects I encode still return nil when I decode them. I have updated the code snippet to include the bits I left out. – Tiddly Jun 26 '13 at 21:26
  • Also cheers for the advice on the convenience methods, I will look into implementing them. I don't think switching to that will fix my problem for the moment tho. – Tiddly Jun 26 '13 at 21:28