ios 沙盒 plist 数据的读取和存储

时间:2022-05-07 22:12:09

转自:http://www.maxiaoguo.com/clothes/230.html

plist 只能存储基本的数据类型 和 array  字典


- (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);
}