Question

I have a problem with loading an Object with NSKeyedUnarchiever.
My Object is a NSDictionary, which contains several Objects like Arrays and some of my own. They all conform to the NSCoding Protocol.

I save my Object like this:

[NSKeyedArchiver archiveRootObject:myDictionary toFile:path];

If I load the Object in the same run with this code:

myDictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

everything works.
But when I quit the App and open it again, it crashes right at this Line.

Does anyone have an idea what is wrong?

Était-ce utile?

La solution

Make sure that your save is successful:

if (![NSKeyedArchiver archiveRootObject:myDictionary toFile:path])
{
    // saving failed for some reason
    return;
}

Also make sure the file exists before trying to unarchive it:

if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
    // file does not exist at path (deleted?)
    return;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top