数据持久化

来源:互联网 发布:vba 数组定义同时赋值 编辑:程序博客网 时间:2024/06/06 03:11

#warning 总结:数据持久化的步骤

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

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

    //3.拼接文件路径

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

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


     苹果手机为了保证自己的数据上的绝对安全设计了沙盒文件,每一个应用程序都配备了自己的沙盒文件,每一次运行,文件夹得名字都会变成一个没有规律的字符串

    第一个参数:当前要前往哪个文件夹,前往documents文件要用NSDocumentDirectory,64行哪个,还可以前往caches文件对应68

     第二个参数:访问的文件类型,指定访问是用户文件夹

    第三个参数:绝对路径(YES),相对(NO)

     绝对路径是给系统使用的,系统可以根据当前的路径找到文件夹,我们在操作文件夹的时候是绝对路径

     相对路径只会把要前往的文件夹显示,其他部分都是~,告诉程序员要去哪个文件夹

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

    NSLog(@"%@", sandBox);

     沙盒一共三个文件

     1document文件:主要是用来存储用户的一些信息,比如收藏的信息或者自己设置的一些内容,所以我们做收藏就是这个文件夹里写文件

     2Library文件夹里是方便程序开发者使用的

     caches 用来保存缓存文件.SDWebImage会把图片加到缓存文件中清除缓存功能酒吧这个文件夹删除

     preferences一般用阿里保存程序员设置的信息,比如NSUserDefaults会把数据保存在这个文件夹里

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

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

    把简单的对象写入到本地,简单对象指的是NSString,NSArray


    // 1.先通过数组获得沙盒路径

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

   NSString *sandBoxPath = sandBox[0];

//     要给写入的文件拼接一个路径,拼接方式有两种

//    NSString *documentPath = [sandBoxPath stringByAppendingString:@"/顾宇.txt"];

    // 第二种

   NSString *documentPath = [sandBoxPathstringByAppendingPathComponent:@"顾宇.txt"];

   NSLog(@"%@", documentPath);

   NSString *str =@"隔壁老顾总耍剑";

    //第一个参数:文件要保存的路径

    // 第二个参数:文件进行保护,yes

    // 第三个参数:编码

    // :错误信息

    [str writeToFile:documentPathatomically:YESencoding:NSUTF8StringEncodingerror:nil];


    //复杂对象写入到本地,主要指我们自己创建的对象写入到本地,也叫归档和反归档操作;

    Student *stu = [Student studentWithName:@"张三" sex:@"man" age:32 hobby:@"lolita"];

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

    NSString *stuDoucumentPath = [stuArr[0] stringByAppendingPathComponent:@"你傻啊.avi"];

    // 对对象进行归档

    // 1对象,2路径

    [NSKeyedArchiver archiveRootObject:stu toFile:stuDoucumentPath];

    NSLog(@"%@", stuDoucumentPath);

    // 反归档操作

    Student *tempStu = [NSKeyedUnarchiver unarchiveObjectWithFile:stuDoucumentPath];

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


类里面

- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoderencodeObject:self.nameforKey:@"name"];

    [aCoder encodeInteger:self.ageforKey:@"age"];

    [aCoderencodeObject:self.sexforKey:@"sex"];

    [aCoderencodeObject:self.hobbyforKey:@"hobby"];

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

}

- (id)initWithCoder:(NSCoder *)aDecoder{

   self = [superinit];

   if (self) {

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

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

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

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

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

    }

    return self;

}


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

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

   NSString *sandBoxPath = sandBox[0];

    NSFileManager *manger = [NSFileManagerdefaultManager];

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

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

    [manger createDirectoryAtPath:filePathwithIntermediateDirectories:YESattributes:nilerror:nil];


    [mangerremoveItemAtPath:filePatherror:nil];

    NSString *cachePath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];

    [mangerremoveItemAtPath:cachePatherror:nil];


    // 清除缓存

    [mangerremoveItemAtPath:filePath error:nil];

    NSString *cachePath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)[0];

    [mangerremoveItemAtPath:cachePath error:nil];


0 0