数据持久化之.plist文件
2016-04-18 11:57
501 查看
原文链接:/article/2449308.html
文件目录简单说明:
应用程序包:包含了所有的资源文件和可执行文件
Document:保存应用运行时生成的需要持久化的数据,iTunes 同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
tmp:保存应用运行时所需的临时数据,使用完毕后再将相应地文件从该目录删除,应用没有运行时,系统也可能会清除该目录下得所有文件。iTunes 同步设备时不会备份该目录。
Library / Caches:保存应用运行时生成的需要持久化的数据,iTunes 同步设备时不会备份该目录。一般存储体积大,不需要备份的非重要数据。
Library / Preference:保存应用的所有偏好设置,ios 的 Setting (设置)应用会在该目录中查找应用的设置信息。iTunes 同步设备时会备份该目录。
下面是利用字典将数据写入到.plist文件
[cpp]
view plain
copy
print?
// // ViewController.m // plist // // Created by Rio.King on 13-9-22. // Copyright (c) 2013年 Rio.King. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createPlist]; [self readPlist]; } -(void)readPlist{ //搜索Document路径 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"%@",dict); } -(void)createPlist{ NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:@"chaoyuan" forKey:@"name"]; [dict setObject:[NSNumber numberWithInt:21] forKey:@"age"]; [dict setObject:@"www.chaoyuan.sinaapp.com" forKey:@"homepage"]; //获取Document目录 NSString *home = NSHomeDirectory(); NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; NSLog(@"%@",documents); NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"]; //写到.plist文件中去 [dict writeToFile:path atomically:YES]; } @end
注意:
属性列表是一种XML格式的文件,拓展名为 plist
如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等基本类型,就可以使用 writeToFile:atomically:方法直接将对象写到属性列表文件中。
附注:
苹果Mac OS X操作系统下,隐藏文件是否显示有很多种设置方法,最简单的要算在Mac终端输入命令。显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写):显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
或者
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles YES
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles NO
输完单击Enter键,退出终端,重新启动Finder就可以了
重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->重新启动
文件目录简单说明:
应用程序包:包含了所有的资源文件和可执行文件
Document:保存应用运行时生成的需要持久化的数据,iTunes 同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
tmp:保存应用运行时所需的临时数据,使用完毕后再将相应地文件从该目录删除,应用没有运行时,系统也可能会清除该目录下得所有文件。iTunes 同步设备时不会备份该目录。
Library / Caches:保存应用运行时生成的需要持久化的数据,iTunes 同步设备时不会备份该目录。一般存储体积大,不需要备份的非重要数据。
Library / Preference:保存应用的所有偏好设置,ios 的 Setting (设置)应用会在该目录中查找应用的设置信息。iTunes 同步设备时会备份该目录。
下面是利用字典将数据写入到.plist文件
[cpp]
view plain
copy
print?
// // ViewController.m // plist // // Created by Rio.King on 13-9-22. // Copyright (c) 2013年 Rio.King. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createPlist]; [self readPlist]; } -(void)readPlist{ //搜索Document路径 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"%@",dict); } -(void)createPlist{ NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:@"chaoyuan" forKey:@"name"]; [dict setObject:[NSNumber numberWithInt:21] forKey:@"age"]; [dict setObject:@"www.chaoyuan.sinaapp.com" forKey:@"homepage"]; //获取Document目录 NSString *home = NSHomeDirectory(); NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; NSLog(@"%@",documents); NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"]; //写到.plist文件中去 [dict writeToFile:path atomically:YES]; } @end
// // ViewController.m // plist // // Created by Rio.King on 13-9-22. // Copyright (c) 2013年 Rio.King. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createPlist]; [self readPlist]; } -(void)readPlist{ //搜索Document路径 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"%@",dict); } -(void)createPlist{ NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:@"chaoyuan" forKey:@"name"]; [dict setObject:[NSNumber numberWithInt:21] forKey:@"age"]; [dict setObject:@"www.chaoyuan.sinaapp.com" forKey:@"homepage"]; //获取Document目录 NSString *home = NSHomeDirectory(); NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; NSLog(@"%@",documents); NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"]; //写到.plist文件中去 [dict writeToFile:path atomically:YES]; } @end
注意:
属性列表是一种XML格式的文件,拓展名为 plist
如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等基本类型,就可以使用 writeToFile:atomically:方法直接将对象写到属性列表文件中。
附注:
mac系统如何显示和隐藏文件
苹果Mac OS X操作系统下,隐藏文件是否显示有很多种设置方法,最简单的要算在Mac终端输入命令。显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写):显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
或者
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles YES
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles NO
输完单击Enter键,退出终端,重新启动Finder就可以了
重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->重新启动
相关文章推荐
- C#测量程序运行时间及cpu使用时间实例方法
- Android手势(2)---多点触控基础之二
- Node.js中使用Markdown
- maven
- datetime和timetamp
- ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet' bytes
- 解决C#程序只允许运行一个实例的几种方法详解
- JavaWeb学习路线
- AngularJS入门教程之AngularJS表达式
- Logistic Regression逻辑回归分类器weka实现
- Linux chkconfig 命令
- storm错误:msg:Field drpc.servers must be an Iterable of java.lang.String
- WebDriver源码分析
- [C++]关于STL慎重选择删除元素的方法
- <<程序员面试宝典>>读书笔记 1
- Oracle_动态sql为本地变量赋值
- hdu3511-Prison Break
- [ASP.NET MVC] Real-time之HTML5 服务器发送事件(server-sent event)
- iOS 多语言支持
- 查看服务器上文件占用磁盘容量