您的位置:首页 > 移动开发 > IOS开发

ios应用数据存储方式(XML属性列表-plist)

2016-05-13 09:25 609 查看
一.ios应用常用的数据存储方式

1.plist(XML属性列表归档)

2.偏好设置

3.NSKeydeArchiver归档(存储自定义对象)

4.SQLite3(数据库,关系型数据库,不能直接存储对象,要编写一些数据库的语句,将对象拆分存储)

5.Core Data(对象型的数据库,把内部环节屏蔽)

应用沙盒

每个ios应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其它文件系统隔离。应用必须待在自己到沙盒里,其他应用不能访问该沙盒(提示:在ios8中已经开发访问)。

应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Layer)



模拟器应用用沙盒的根路径在:(apple是用用户名,7.0是模拟器版本)/Users/apple/Library/Application Support/iPhone/Simulator/7.0/Applications

应用沙盒结构分析

应用程序包:(上图中的Layer)包含了所有的资源文件和可执行文件。

Documents:保存应用运行时生成的需要持久性的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录。

tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录

Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大,不需要备份的非重要数据。

Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录。

四.应用沙盒常见的获取方式

1.沙盒根目录:NSString *home = NSHomeDirectory(); Documents;(2种方式)

2.利用沙盒根目录拼接”Documents”字符串

NSString *home = NSHomeDirectory();

NSString *documents = [home stringByAppendingPathComponent:@”Documents”];//不建议采用,因为新版本的操作系统可能会修改目录名

3.利用NSSearchPathForDirectoriesInDomains函数

//NSUserDomainMask代表从用户文件下找

//YES代表展开路径中的波浪字符”~”

NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,NO);//在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素

NSString *documents = [array objectAtIndex:0];

4.tmp:NSString *tmp = NSTemporaryDirectory();

5.Library/Caches:(跟Documents类似的2种方法)

6.利用沙盒根目录拼接”Caches”字符串

7.利用NSSearchPathForDirectoriesInDomain函数(将函数的2个参数改为:NSCachesDirectory即可)

8.Library/Preference:通过NSUserDefaults类存取该目录下的设置信息

代码:

#define CURRENT_SCREEN_WIDTH     [UIScreen mainScreen].bounds.size.width
#define CURRENT_SCREEN_HEIGHT     ([UIScreen mainScreen].bounds.size.height - 64)
#define BUTTON_WIDTH     80
#define BUTTON_HEIGHT    40

@interface ViewController ()

//保存数据按钮
@property(nonatomic,strong) UIButton *saveButton;
//读取数据按钮
@property(nonatomic,strong) UIButton *readButton;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self initControl];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

//初始化控件
- (void)initControl{
_saveButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
CURRENT_SCREEN_HEIGHT/2 - BUTTON_HEIGHT,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_saveButton setTitle:@"保存数据" forState:UIControlStateNormal];
[_saveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_saveButton addTarget:self
action:@selector(saveClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_saveButton];

_readButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2,
_saveButton.frame.origin.y + _saveButton.frame.size.height + 60,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_readButton setTitle:@"读取数据" forState:UIControlStateNormal];
[_readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_readButton addTarget:self
action:@selector(readClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_readButton];

}

- (void)saveClick{
//获取应用程序目录
NSString *home = NSHomeDirectory();
NSLog(@"应用程序目录:%@",home);

//NSUserDomainMask在用户目录下查找
//YES 代表用户目录的~
//NSDocumentDirectory查找Documents文件夹
//建议使用如下方法动态获取
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSLog(@"Documents文件夹路径:%@",doc);

//拼接文件路径
NSString *path = [doc stringByAppendingString:@"/abc.plist"];

//NSArray *array = @[@"ios",@"23"];
//[array writeToFile:path atomically:YES];

//NSDictionary *dict = @{@"name":@"ios",@"age":@"28"};
//[dict writeToFile:path atomically:YES];

/*
plist只能存储系统自带的一些常规的类,也就是有writeToFile方法的对象才可以使用plist保持数据
字符串/字典/数据/NSNumber/NSData ...
*/

//自定义的对象不能保存到plist中
DBPerson *person = [[DBPerson alloc] init];
person.name = @"ios";

}

- (void)readClick{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [doc stringByAppendingString:@"/abc.plist"];
//读取数据
//NSArray *array = [NSArray arrayWithContentsOfFile:path];
//NSLog(@"%@",array);

//NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
//NSLog(@"name = %@",[dict objectForKey:@"name"]);
//NSLog(@"age = %@",[dict objectForKey:@"age"]);

}

@end


五.属性列表

1.属性列表是一种XML格式的文件,拓展名为plist。

2.如果对象是NSString,NSDictionary,NSArray,NSData,NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中

3.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: