I want to archive a dictionary containing just NSStrings, but when I unarchive, an error occurred:
[NSKeyedUnarchiver initForReadingWithData:]: non-keyed archive cannot be decoded by NSKeyedUnarchiver
My code is:
- (IBAction)startArchive:(id)sender {
NSString *path = [self pathForDataFile];
NSMutableDictionary *ageDictionary= [NSMutableDictionary dictionary];
[ageDictionary setValue:@"12" forKey:@"Jack"];
[ageDictionary setValue:@"22" forKey:@"John"];
[NSArchiver archiveRootObject:ageDictionary toFile:path];
}
- (IBAction)unarchive:(id)sender {
NSString *path = [self pathForDataFile];
NSDictionary *rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@",rootObject);
}
The reason is Unarchiver. You used NSArchiver to archiver. As unarchiver you should use NSUnarchiver. If you use NSKeyedArchiver, you should use NSKeyedUnarchiver.
You’re confused them.
flowing codes are test work:
=============
this is logs:
2012-08-27 14:03:11.511 Untitled[7930:707] {
Jack = 12;
John = 22;
}