saving NSMutable Array containing View Controllers
I am new to ObjC and iPhone SDK so I am learning by tinkering with the sample code.
I took UICatalog and added row delete and reordering. I also added a detail disclosure set of view controllers.
Now, how do I save the two mutable arrays so the table view is the way it was left? Here is what I am currently trying.
//Saving routine.
NSMutableData *data = [NSMutableData data];
NSMutableData *dataB = [NSMutableData data];
NSKeyedArchiver *encode = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSKeyedArchiver *encodeB = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSString* docsPathA = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* docsPathB = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath = [docsPathA stringByAppendingPathComponent:@"SavedMenuState"];
NSString* filePathB = [docsPathB stringByAppendingPathComponent:@"SavedDetailState"];
[encode encodeObject:menuList forKey:@"SavedMenuList"];
[data writeToFile:filePath atomically:YES];
[encodeB encodeObject:detailList forKey:@"SavedDetailList"];
[dataB writeToFile:filePathB atomically:YES];
[encode finishEncoding];
[encode release];
Then to retrieve upon relaunch.
- (void)awakeFromNib
{
//retrieve saved state
NSString* docsPathA = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* docsPathB = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath = [docsPathA stringByAppendingPathComponent:@"SavedMenuState"];
NSString* filePathB = [docsPathB stringByAppendingPathComponent:@"SavedDetailState"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath])
{
NSMutableData *data;
NSMutableData *dataB;
NSKeyedUnarchiver *decoder;
NSKeyedUnarchiver *decoderB;
data = [NSData dataWithContentsOfFile:filePath];
decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
self.menuList = [[NSMutableArray alloc] initWithCapacity:13];
self.menuList = [decoder decodeObjectForKey:@"SavedMenuList"];
dataB = [NSData dataWithContentsOfFile:filePathB];
decoderB = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataB];
self.detailList = [[NSMutableArray alloc] initWithCapacity:13];
self.detailList = [decoderB decodeObjectForKey:@"SavedDetailList"];
}
else{
//Main View Selections
self.menuList = [[NSMutableArray alloc] initWithCapacity:13];
//Here is the list of view controllers...
Upon running, the app first works fine, I edit the rows, quit, relaunch, then I just get a blank table view.
What gives?
|