/ Published in: Objective C
This is a follow up to another snippet which shows how to create the object you want to archive and unarchive.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* INTERFACE */ #import <Foundation/Foundation.h> #define kFilename @"dataarchive.plist" #define kDataKey @"Data" @class DataModel; DataModel *dataModel; } @property (nonatomic, retain) DataModel *dataModel; -(void)initDataModel; +(DataManager *)sharedDataManager; @end /* IMPLEMENTATION */ #import "DataManager.h" #import "SynthesizeSingleton.h" #import "DataModel.h" #import "FaveVO.h" @implementation DataManager SYNTHESIZE_SINGLETON_FOR_CLASS(DataManager); @synthesize dataModel; //----------------------------------------------------------------------------------------------------- { return [documentsDirectory stringByAppendingPathComponent:kFilename]; } //----------------------------------------------------------------------------------------------------- -(void)initDataModel { { NSLog(@"FILE EXISTS"); DataModel *decodedDataModel = [unarchiver decodeObjectForKey:kDataKey]; dataModel = [[DataModel alloc] init]; NSEnumerator *enumerator; enumerator = [decodedDataModel.favesArray objectEnumerator]; FaveVO *decodedFaveVO; while (decodedFaveVO = [enumerator nextObject]) { [dataModel createNewFaveVOFromFaveVO:decodedFaveVO]; } int index = decodedDataModel.faveIndex; dataModel.faveIndex = index; dataModel.currentFave = [dataModel.favesArray objectAtIndex:index]; [unarchiver finishDecoding]; [unarchiver release]; [data release]; } else { NSLog(@"FILE DOES NOT EXIST"); dataModel = [[DataModel alloc] init]; } UIApplication *app = [UIApplication sharedApplication]; selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app]; } //------------------------------------------------------------- { NSLog(@"Application will terminate"); [archiver encodeObject:dataModel forKey:kDataKey]; [archiver finishEncoding]; [data writeToFile:[self dataFilePath] atomically:YES]; [archiver release]; [data release]; } //-------------------------------------------------- -(void)dealloc { [dataModel release]; [super dealloc]; } @end