xcode
创建两个按钮 一个写,一个读。
进入 故事版 两个按钮进行拖线,实现写方法和读方法。
实现写方法:- (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(@'已写入'); }
实现读方法:- (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); }
数据是打印出来的:
ok 没什么要注意的,拼接时别错了就好。