Saving and Loading the Checklist Items

来源:互联网 发布:win10电脑无法识别网络 编辑:程序博客网 时间:2024/05/17 22:29

Saving


- (NSString *)documentsDirectory

{

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);//asd

    NSString *documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}


- (NSString *)dataFilePath

{

    return [[selfdocumentsDirectory] stringByAppendingPathComponent:@"Checklists.plist"]; //Create new string  asd/Checklists.plist

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    NSLog(@"Documents folder is %@",[selfdocumentsDirectory]);

    NSLog(@"Data file path is %@", [selfdataFilePath]);

...

}


pp154




Loading

pp164

- (void)loadChecklistsItems

{

    NSString *path = [selfdataFilePath];

    if ([[NSFileManagerdefaultManager] fileExistsAtPath:path]) {

        NSData *data = [[NSDataalloc] initWithContentsOfFile:path];

        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:data];

        items = [unarchiver decodeObjectForKey:@"ChelistItems"];

    } else {

        items = [[NSMutableArrayalloc] initWithCapacity:20];

    }

}


    //restore       For viewcontrollers that are automatically loaded from a nib or storyboard

- (id)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [superinitWithCoder:aDecoder]) {

        [selfloadChecklistsItems];

    }

    

    return self;

}