标签:last 文件路径 ini tor support component domain list sea
/** * 点击保存按钮 */ - (IBAction)saveDataBtnClick:(id)sender { // youtube做法 // NSString *path = @"/Users/apple/Library/Application Support/iPhone Simulator/7.1/Applications/A6D53E11-DDF0-4392-B2D4-FE77A96888A6/Documents/abc.plist"; // 获取应用程序沙河目录 NSString *home = NSHomeDirectory(); // 不建议写/ // NSString *path = [home stringByAppendingString:@"/Documents"]; // 不建议Documents写死 // NSString *path = [home stringByAppendingPathComponent:@"Documents"]; // NSUserDomainMask 在用户目录下查找 // YES 代表用户目录的~ // NSDocumentDirectory 查找Documents文件夹 // 建议使用如下方法动态获取 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 拼接文件路径 NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"] ; NSLog(@"%@", path); // NSArray *arr = @[@"lnj", @"28"]; // [arr writeToFile:path atomically:YES]; // NSDictionary *dict = @{@"name": @"lnj", @"age":@"28"}; // 调用writeToFile将数据写入文件 // [dict writeToFile:path atomically:YES]; /* plist只能存储系统自带的一些常规的类, 也就是有writeToFile方法的对象才可以使用plist保存数据 字符串/字典/数据/NSNumber/NSData ... */ // 自定义的对象不能保存到plist中 NJPerson *p = [[NJPerson alloc] init]; p.name =@"lnj"; NSDictionary *dict = @{@"person": @"abc"}; [dict writeToFile:path atomically:YES]; } /** * 点击读取按钮 */ - (IBAction)readDataBtnClick:(id)sender { NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"] ; // 读取数据 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"%@", dict); }
标签:last 文件路径 ini tor support component domain list sea
原文地址:http://www.cnblogs.com/liuwj/p/6528076.html