存数据 - (ibaction)save:(id)sender { - 获取沙盒根根路径 nsstring *homedir = nshomedirectory(); 在某个范围内搜索文件夹的路径. directory:获取哪个文件夹 domainmask:在哪个路径下搜索 expandtilde:是否展开路径. 这个方法获取出的结果是一个数组.因为有可以搜索到多个路径. nsarray *array = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes); 在这里,我们指定搜索的是cache目录,所以结果只有一个,取出cache目录 nsstring *cachepath = array[0]; 拼接文件路径 nsstring *filepathname = [cachepath stringbyappendingpathcomponent:@"ageplist.plist"]; 想要把这个字典存储为plist文件. 直接把字典写入到沙盒当中 用字典写, plist文件当中保存的是字典. nsdictionary *dict = @{@"age" : @18,@"name" : @"gaowei"}; 获取沙盒路径 tofile:要写入的沙盒路径 [dict writetofile:filepathname atomically:yes]; 用数组写,plist文件当中保存的类型是数组. nsarray *dataarray = @[@56,@"asdfa"]; 获取沙盒路径 tofile:要写入的沙盒路径 [dataarray writetofile:filepathname atomically:yes]; } 读取数据 - (ibaction)reader:(id)sender { - 这个方法获取出的结果是一个数组.因为有可以搜索到多个路径. nsarray *array = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes); 在这里,我们指定搜索的是cache目录,所以结果只有一个,取出cache目录 nsstring *cachepath = array[0]; 拼接文件路径 nsstring *filepathname = [cachepath stringbyappendingpathcomponent:@"ageplist.plist"]; 从文件当中读取字典, 保存的plist文件就是一个字典,这里直接填写plist文件所存的路径 nsdictionary *dict = [nsdictionary dictionarywithcontentsoffile:filepathname]; 如果保存的是一个数组.那就通过数组从文件当中加载. nsarray *dataarray = [nsarray arraywithcontentsoffile:filepathname]; }
// 保存用户信息至沙盒 nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults]; // 设置需要写入的数据 [defaults setobject:self.accounttextf.text forkey:azaccountinfo]; [defaults setobject:self.pwdtextf.text forkey:azpwdinfo]; [defaults setbool:self.rempwdswitch.ison forkey:azreminfo]; [defaults setbool:self.autologinswitch.ison forkey:azautologininfo]; // 立即写入文件 [defaults synchronize];取出数据
// 从沙盒中取出用户保存的设置 nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults]; // 通过用户保存的设置进行相应的设置 self.rempwdswitch.on=[defaults boolforkey:azreminfo];
//获取沙盒临时目录 nsstring *temppath = nstemporarydirectory(); nsstring *filepath = [temppath stringbyappendingpathcomponent:@"persion.data"]; // 写入数据 [nskeyedarchiver archiverootobject:self.contactorarray tofile:filepath];archiverootobject这个方法底层会去调用保存对象的encodewithcoder方法,去询问要保存这个对象的哪些属性.所以要实现encodewithcoder方法, 告诉其要保存这个对象的哪些属性. encodewithcoder方法在nscoding文件中,如果文件父类是nsobject,还需要继承nscoding @interface azcontactor : nsobject
// 编码 - (void)encodewithcoder:(nscoder *)acoder { [acoder encodeobject:self.contactname forkey:@"name"]; [acoder encodeobject:self.phonenum forkey:@"num"]; }可以将文件沙盒路径抽成宏,这样在文件的存取过程中就不会出错
#define azdatapath [nssearchpathfordirectoriesindomains(nslibrarydirectory, nsuserdomainmask, yes).lastobject stringbyappendingpathcomponent:@"contactor.data"]在取出数据的unarchiveobjectwithfile:方法中,在底层会去调用-(instancetype)initwithcoder:(nscoder *)adecoder方法进行解码,需要重写这个方法对数据进行解析
// 解码 -(instancetype)initwithcoder:(nscoder *)adecoder { if (self=[super init]) { self.contactname=[adecoder decodeobjectforkey:@"name"]; self.phonenum=[adecoder decodeobjectforkey:@"num"]; } return self; } // 取出数据 nsmutablearray *data=[nskeyedunarchiver unarchiveobjectwithfile:azdatapath];
如对本文有疑问, 点击进行留言回复!!
解决Android10读取不到/sdcard/、/storage/emulated/0/文件的问题
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
IOS开发教程第一季之UI进阶day2合并IOS学习013--App启动过程、多控制器管理
网友评论