Search code examples
iosnskeyedarchivernskeyedunarchiver

Does NSKeyedArchiver overwrite the previous archive value?


I am using the following to do IOS App data backup

  • NSKeyedArchiver.archiveRootObject()
  • NSKeyedUnarchiver.unarchiveObjectWithFile()

Based on the Apple documentation here -

I was curious here with a potentially silly question - does the previous archive get deleted each time you make a new save?

Question

  • When calling NSKeyedArchiver.archiveRootObject() does the new archive overwrite the previous archive value?
  • If it doesn't overwrite the old value do you need to delete the old value?

Here is a useful reference on deleting NSKeyedArchiver values


Solution

  • NSKeyedArchiver is not responsible to overwrite the previous object. It just convert youObject to NSData and vice versa. Because, 'Custom Object' can't be save to NSUserDefaults.

    additionay, to overrite data you should do this.i.e

     [userDefaults setObject:data forKey:identifier];
     [userDefaults synchronize];
    

    Remove data:

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:identifier];
    [[NSUserDefaults standardUserDefaults] synchronize];