iOS开发之数据的写入与读取(plist存储)

- (IBAction)save {

    [self saveArray];

}


- (void)saveArray

{

    // 1.获得沙盒根路径

    NSString *home = NSHomeDirectory();

    

    // 2.document路径

    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];

    

    // 3.新建数据

//    MJPerson *p = [[MJPerson alloc] init];

//    p.name = @"rose";

    NSArray *data = @[@"jack",@10, @"ffdsf"];

    

    

    NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];

    

    

    [data writeToFile:filepath atomically:YES];

}


- (IBAction)read {

    // 1.获得沙盒根路径

    NSString *home = NSHomeDirectory();

    

    // 2.document路径

    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];

    

    // 3.文件路径

    NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];

    

    // 4.读取数据

    NSArray *data = [NSArray arrayWithContentsOfFile:filepath];

    NSLog(@"%@", data);

}

你可能感兴趣的:(iOS开发之数据的写入与读取(plist存储))