iOS开发plist存储

// 当点点击保存的时候调用
- (IBAction)save:(id)sender {
    // 获取沙盒的根路径
    NSString *docPath =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%@",docPath);

    // 拼接文件路径
    NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];

    // 只有具备writeToFile:的对象才能使用plist存储,NSArray
    NSArray *array = @[@"数据1",@"数据2",@"数据3"];

    [array writeToFile:filePath atomically:YES];
}

//当点击读取的时候调用
- (IBAction)read:(id)sender {
    NSString *docPath =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%@",docPath);

    // 拼接文件路径
    NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
   NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
    NSLog(@"%@",data);

}

你可能感兴趣的:(ios,开发,存储,plist)