【深入浅出ios开发】数据存储-----XML属性列表(plist)归档

来源:互联网 发布:如何做到处事不惊 知乎 编辑:程序博客网 时间:2024/05/22 05:34

①存储数据:

1.获得home路径:

NSString *home = NSHomeDirectory();

2.通过home路径获得Documents/xxx.plist路径

3.往相应的路径写数据

- (IBAction)save:(id)sender {    NSString *home = NSHomeDirectory();    NSLog(@"%@",home);        NSString *strDocument = [home stringByAppendingPathComponent:@"Documents/xml.plist"];        NSArray *array = @[@"A",@"B",@"C"];    [array writeToFile:strDocument atomically:YES];}

②读取数据:

1.获得相应的plist文件的路径

2.直接通过类相应的方法调用读取数据

- (IBAction)read:(id)sender {    NSString *home = NSHomeDirectory();    NSLog(@"%@",home);        NSString *strDocument = [home stringByAppendingPathComponent:@"Documents/xml.plist"];    NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:strDocument];        NSLog(@"%@",array);    }


0 0
原创粉丝点击