多语言展示
当前在线:1825今日阅读:2今日分享:38

ios 数据持久化XML属性列表归档plist存储读取

ios软件实现数据持久化,plist文件的存储与读取。
工具/原料

xcode

方法/步骤
1

创建两个按钮 一个写,一个读。

2

进入 故事版 两个按钮进行拖线,实现写方法和读方法。

3

实现写方法:- (IBAction)write {//    NSLog(@'write');    //1.获取沙盒路径    NSString *home = NSHomeDirectory();//    NSLog(@'%@',home);        //2.拼接document 路径    NSString *docPath = [home stringByAppendingPathComponent:@'Documents'];//    NSLog(@'%@',docPath);        //新建数据    NSArray *data = @[@'jake',@100,@'good'];        NSString *filepath = [docPath stringByAppendingPathComponent:@'name.plist'];    NSLog(@'%@',filepath);    // /Users/macbookair/Library/Developer/CoreSimulator/Devices/1C2C40E0-FCEA-4303-B32C-063272C2E940/data/Containers/Data/Application/113D8801-EE55-41A9-88EA-1EEF90E8D6A6/Documents/name.plist       //写入数据    [data writeToFile:filepath atomically:YES];        NSLog(@'已写入');    }

4

实现读方法:- (IBAction)read {//    NSLog(@'read');    //1.获取沙盒路径    NSString *home = NSHomeDirectory();        //2.拼接document 路径    NSString *docPath = [home stringByAppendingPathComponent:@'Documents'];        //3.文件路径    NSString *filepath = [docPath stringByAppendingPathComponent:@'name.plist'];        //4.读取数据    NSArray *data = [NSArray arrayWithContentsOfFile:filepath];        NSLog(@'%@',data);    }

5

数据是打印出来的:

注意事项

ok 没什么要注意的,拼接时别错了就好。

推荐信息