I am having trouble with NSKeyedArchiver. I create an archive with
archive = [NSDictionary dictionaryWithObjectsAndKeys:
pruneTableP2, @"depth table",
nil];
archiveData = [NSKeyedArchiver archivedDataWithRootObject: archive
requiringSecureCoding: NO
error: nil];
I then get a "The data isn’t in the correct format" error when I go to unarchive it with unarchivedObjectOfClass:. The subsequent call to a depreciated method works however.
archive = [NSKeyedUnarchiver unarchivedObjectOfClass: [NSDictionary class]
fromData: fileData
error: &error];
if( archive == nil )
{
archive = [NSKeyedUnarchiver unarchiveObjectWithData: fileData];
NSLog( @"%@", [error localizedFailureReason]);
}
What am I doing wrong?
Using the more explicit method: unarchivedDictionaryWithKeysOfClasses:objectsOfClasses:fromData:error: eliminates all the problems. The following code executes with no errors.
-(void)test: (NSString *)input
{
NSDictionary *source,
*destination;
NSSet *classes;
NSMutableData *mData1,
*mData2;
NSData *table1,
*table2,
*archive;
NSError *error;
@autoreleasepool
{
mData1 = [NSMutableData dataWithData: edgeTableC3v];
mData2 = [NSMutableData dataWithData: cornerTableC3v];
table1 = mData1;
table2 = mData2;
source = [NSDictionary dictionaryWithObjectsAndKeys:
input, @"input",
table1, @"edge table",
table2, @"corner table",
nil];
archive = [NSKeyedArchiver archivedDataWithRootObject: source
requiringSecureCoding: YES
error: &error];
if( error != nil )
{
[self report: [NSString stringWithFormat: @"\nError creating archive\n\t%@", [error localizedFailureReason]]];
}
else
{
classes = [NSSet setWithObjects:
[NSData class],
[NSString class],
nil];
destination = [NSKeyedUnarchiver unarchivedDictionaryWithKeysOfClasses: [NSSet setWithObject: [NSString class]]
objectsOfClasses: classes
fromData: archive
error: &error];
if( error != nil)
{
[self report: [NSString stringWithFormat: @"\nError reading archive\n\t%@", [error localizedFailureReason]]];
}
else
[self report: [NSString stringWithFormat: @"\nNo Error\n%@", [destination description]]];
}
[self reportDone];
}
}
OUTPUT
No Error
{
"corner table" = {length = 2449440, bytes = 0x88a40000 e0970000 98c40000 48b70000 ... f3b00000 85a40000 };
"edge table" = {length = 145152, bytes = 0x00000000 00000000 140b0000 250a0000 ... 99090000 bf080000 };
input = Parameters;
}
Note that I archived NSMutableData objects and they were unarchived with no error. The runtime doesn't send any warnings when unarchiving.