PDA

View Full Version : How to save archive data to plist




mikezang
Aug 11, 2010, 04:14 AM
I am reading "Beginning iPhone 3 Development", there is a sample to read archive data in plist as below but no code for save data. I want to save data back to plist after I modified data in self.list, what can I do?

#import "PresidentsViewController.h"
#import "PresidentDetailController.h"
#import "President.h"

@implementation PresidentsViewController
@synthesize list; - (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents"*ofType:@"plist"];
NSData *data;
NSKeyedUnarchiver *unarchiver;

data = [[NSData alloc] initWithContentsOfFile:path];
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSMutableArray *array = [unarchiver decodeObjectForKey:@"Presidents"];
self.list = array;
[unarchiver finishDecoding];
[unarchiver release];
[data release];
}



robbieduncan
Aug 11, 2010, 04:51 AM
Read the Archives and Serialization (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i) documentation and learn how to do the reverse of the above.

I'm not about to post the code for you: you'll learn nothing from simply copying others code and thinking you now know how to write it. The only way to learn is to write the code yourself, make mistakes, learn how to debug and then learn from those mistakes.

mikezang
Aug 11, 2010, 05:48 AM
Thanks for your link, I will read it.