欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-01-13 21:43:29
一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) 4.SQLite3(数据库,关系型数据库,不能直接存储对象,要编写一些数据库的语句,将对象拆分存储) 5.Core Data(对象型的数据库,把内部环节屏蔽 ......

 

一.ios应用常用的数据存储方式 
  1.plist(xml属性列表归档) 
  2.偏好设置 
  3.nskeydearchiver归档(存储自定义对象) 
  4.sqlite3(数据库,关系型数据库,不能直接存储对象,要编写一些数据库的语句,将对象拆分存储) 
  5.core data(对象型的数据库,把内部环节屏蔽)

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

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

模拟器应用用沙盒的根路径在: (apple是用用户名,模拟器版本9.4.1)

/users/apple/library/developer/coresimulator/devices/ccbbf90e-03ce-4b92-827f-345635a6cd7e/data/containers/data/application/30ef0702-4062-40f9-b82c-974679878206

 

应用沙盒结构分析  应用程序包:(上图中的layer)包含了所有的资源文件和可执行文件。 
1) documents:保存应用运行时生成的需要持久性的数据,itunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录。 
2) tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。itunes同步设备时不会备份该目录 
3) library 目录:这个目录下有两个子目录:caches 和 preferences
  library/caches:保存应用运行时生成的需要持久化的数据,itunes同步设备时不会备份该目录。一般存储体积大,不需要备份的非重要数据。 
  library/preference:保存应用的所有偏好设置,ios的settings(设置)应用会在该目录中查找应用的设置信息。itunes同步设备时会备份该目录。

4) appnameapp 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。 

 

四.应用沙盒常见的获取方式 
  1.沙盒根目录:nsstring *home = nshomedirectory();  documents目录两种方法

nsstring *homedir = nshomedirectory();

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

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

  3.利用 nssearchpathfordirectoriesindomains 函数 

//nsuserdomainmask代表从用户文件下找 
//yes代表展开路径中的波浪字符”~” 
nsarray *array = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask,yes);//在ios中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素 
nsstring *documents = [array objectatindex:0]; 

  4. 获取 tmp 目录 

nsstring *tmp = nstemporarydirectory(); 

  5.library/caches:(跟documents类似的2种方法) 

nsarray *paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);
nsstring *cachesdir = [paths objectatindex:0];

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

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

  8.library/preference:通过 nsuserdefaults 类存取该目录下的设置信息 

  9.获取应用程序程序包中资源文件路径的方法: 例如获取程序包中一个图片资源(apple.png)路径的方法:

nsstring *imagepath = [[nsbundle mainbundle] pathforresource:@”apple” oftype:@”png”];
uiimage *appleimage = [[uiimage alloc] initwithcontentsoffile:imagepath];

  代码中的mainbundle类方法用于返回一个代表应用程序包的对象。


代码:

#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:方法直接将对象写到属性列表文件中