Saving
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //asd
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
- (NSString *)dataFilePath
{
return [[self documentsDirectory] stringByAppendingPathComponent:@"Checklists.plist"]; //Create new string asd/Checklists.plist
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"Documents folder is %@",[self documentsDirectory]);
NSLog(@"Data file path is %@", [self dataFilePath]);
...
}
pp154
Loading
pp164
- (void)loadChecklistsItems
{
NSString *path = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
items = [unarchiver decodeObjectForKey:@"ChelistItems"];
} else {
items = [[NSMutableArray alloc] initWithCapacity:20];
}
}
//restore For viewcontrollers that are automatically loaded from a nib or storyboard
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self loadChecklistsItems];
}
return self;
}