数据持久化

沙盒里一共有三个文件

// 1.是Documents文件:主要是和用来存储用户想要的存储的一些信息,比如收藏的信息或者自己设置的一些内容,所以我们左的收藏功能就是往这个文件夹里面写文件

// 2. Library文件夹是方便程序开发者使用的,主要操作它里的两个文件夹,caches和Preferences

// caches:用来保存缓存文件,SDWebImage会把图片加到缓存文件中,所以清除缓存功能就会把这个文件夹删除

// Preferences一般来保存程序员设置的信息,比如NSUserDefaults就会把数据保存在这个文件夹里

// 3.tmp文件:一般存放临时内容

//之前在沙盒里还有一个.app文件,在新的版本里已经被移走了

//把简单对象写入到本地

//把复杂对象写入到本地

************************************************************************************************************

#pragma mark签订完NSCoding协议之后,需要实现两个协议方法,一个是归档的时候使用的,另一个是反归档的时候使用的

- (void)encodeWithCoder:(NSCoder*)aCoder

{

// key归档的时候用的要和反归档时候一致

[aCoderencodeObject:self.nameforKey:@"姓名"];

[aCoderencodeInteger:self.ageforKey:@"年龄"];

[aCoderencodeObject:self.sexforKey:@"性别"];

[aCoderencodeObject:self.hobbyforKey:@"爱好"];

//使用encode方法要和数据的类型相互匹配

}

- (id)initWithCoder:(NSCoder*)aDecoder

{

self= [superinit];

if(self) {

//把数据根据之前的key反编译出来

self.name= [aDecoderdecodeObjectForKey:@"姓名"];

self.age= [aDecoderdecodeIntegerForKey:@"年龄"];

self.sex= [aDecoderdecodeObjectForKey:@"性别"];

self.hobby= [aDecoderdecodeObjectForKey:@"爱好"];

}

returnself;

}

********************************************************************************

//创建三个学生

Student*stu1 = [[Studentalloc]initWithName:@"崔磊"sex:@"随便"age:49hobby:@"随便"];

Student*stu2 = [[Studentalloc]initWithName:@"顾宇"sex:@"男"age:40hobby:@"女"];

Student*stu3 = [[Studentalloc]initWithName:@"姗姗"sex:@"男"age:21hobby:@"学习"];

NSArray*arr =@[stu1,stu2,stu3];

NSArray*sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*sandBoxPath = sandBox[0];

//拼接文件路径

NSString*documentPath = [sandBoxPathstringByAppendingPathComponent:@"Student.plist"];

//归档

[NSKeyedArchiverarchiveRootObject:arrtoFile:documentPath];

NSLog(@"%@",documentPath);

//反归档,遍历学生的姓名

NSArray*newArr =  [NSKeyedUnarchiverunarchiveObjectWithFile:documentPath];

for(Student*stuinnewArr) {

NSLog(@"%@",stu.name);

}

*****************************************************************************************

//通过文件管理者对文件夹进行操作

NSArray*sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString*sandBoxPath = sandBox[0];

//创建一个文件管理者

NSFileManager*manage = [NSFileManagerdefaultManager];

//给要创建的文件夹拼接一个路径

NSString*filePath = [sandBoxPathstringByAppendingPathComponent:@"guyu"];

//文件夹名不需要任何的扩展名

//通过manager进行文件夹的创建

[managecreateDirectoryAtPath:filePathwithIntermediateDirectories:YESattributes:nilerror:nil];

NSLog(@"%@",filePath);

//向新创建的文件夹里写入一个字符串

NSString*document = [filePathstringByAppendingPathComponent:@"字符串.txt"];

NSString*str =@"我是一个字符串";

[strwriteToFile:documentatomically:YESencoding:NSUTF8StringEncodingerror:nil];

NSLog(@"%@",document);

//移除文件夹

//    [manage removeItemAtPath:filePath error:nil];

//移除cache

NSArray*cache =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,1,YES);

NSString*cachePath = cache[0];

[manageremoveItemAtPath:cachePatherror:nil];

}

********************************************************************************

总结:数据持久化的步骤

// 1.指定前往哪一个文件夹

// 2.用字符串来接收路径

// 3.拼接文件路径

// 4.写入本地或者归档操作

//如果是复杂对象归档,要签订NSCoding方法,并且实现两个协议方法,放在数组里的复杂对象归档也要签订协议

你可能感兴趣的:(数据持久化)