zoukankan      html  css  js  c++  java
  • 数据存储

    数据存储

    常见存储方法有三种:plist存储、偏好设置(NSUserDefaults)和归档。

    属性列表(plist)存储:

    • 适用对象:只有带有writeToFile:方法的对象才能用这种方法,比如NSString、、NSArray、NSDictionary、NSSet、NSNumber、NSData等,不能存储自定义的对象
    • 存储方法:
      调用对象的writeToFile...方法就可以写入文件
    • 读取方法:
      调用对象的...WithContentsOfFile方法就可以从文件中读取对象内容
    // 存数据
    - (void)save {
       // plist文件存储一般都是存取字典和数组,直接写成plist文件,把它存到应用沙盒当中.
       // 只有在ios当中才有plist存储,它是ios特有的存储方式.
    
        // 获取沙盒根根路径,每一个应用在手机当中都有一个文件夹,这个方法就是获取当前应用在手机里安装的文件.
       // NSString *homeDir = NSHomeDirectory();
       // NSLog(@"homeDir = %@",homeDir);
    
       // 在某个范围内搜索文件夹的路径.
       // 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" : @"iris"};
      //  获取沙盒路径
     //  ToFile:要写入的沙盒路径
        [dict writeToFile:filePathName atomically:YES];
    
       // 用数组写,plist文件当中保存的类型是数组.
        NSArray *dataArray = @[@56,@"asdfa"];
       // 获取沙盒路径
       // ToFile:要写入的沙盒路径
        [dataArray writeToFile:filePathName atomically:YES];
    }
    
    // 读取数据
    - (void)read {
        // 这个方法获取出的结果是一个数组.因为有可能搜索到多个路径.
        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];
        NSLog(@"%@",dataArray);
    }
    

    偏好设置(NSUserDefaults):

    • 适用对象:使用NSUserDefaults,存储用户关于应用的偏好设置,本质上仍然是plist存储,不能存储自定义的OC对象
    • 存储方法:
      利用NSUserDefaultssetObject等方法进行存储
    • 读取方法:
      利用NSUserDefaultsobjectForKey等方法进行读取
    - (void)save {
        // 偏好设置NSUserDefaults,底层就是封闭了一个字典,利用字典的方式生成plist文件
        // 好处:不需要关心文件名(它会自动生成)快速进行键值对存储.
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"iris" forKey:@"name"];
        [defaults setBool:YES forKey:@"isBool"];
        [defaults setInteger:5 forKey:@"num"];
        // 同步,立即写入文件.
        [defaults synchronize];
    }
    - (void)read {
        // 存的时候用什么key存的, 读的时候就要用什么key值取
        // 存的时候使用的什么类型,取的时候也要用什么类型.
        NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
        BOOL isBool  = [[NSUserDefaults standardUserDefaults] boolForKey:@"isBool"];
        NSInteger num = [[NSUserDefaults standardUserDefaults] integerForKey:@"num"];
        NSLog(@"name =%@-isBool=%d-num=%ld",str,isBool,num);
    }
    

    归档(NSKeyedArchiver):

    • 适用对象:可以存储自定义的对象,只有遵守了NSCoding协议的对象才可以
    • 存储方法(归档):
      调用NSKeyedArchiverarchiverRootObject: toFile: 方法存储对象,archiverRootObject执行这个方法时,底层就会调用要存的对象的encodeWithCoder方法,调用encodeWithCoder目的就是想询问下要存对象的哪些属性
    • 读取方法(解档):
      调用NSKeyedArchiverunarchiverObjectWithFile:方法,执行enachiverObjectWithFile:方法时就会调用initWithCoder:方法,调用initWithCoder:方法目的就是询问下要读的对象有那些属性要读,怎么读。
    • 注意:
      当有继承关系时,必须调用父类的归档解档方法,当有组合包含关系时,也必须实现所包含对象类的归档解档方法。
    // 保存数据
    - (void)save {
        // 归档一般都是保存自定义对象的时候,使用归档.因为plist文件不能够保存自定义对象.
        // 如果一个字典当中保存有自定义对象,那么把这个字典写入到文件当中,它是不会生成plist文件的.
        Person *person = [[Person alloc] init];
        person.name = @"iris";
        person.age = 18;
    
        // 获取沙盒临时目录
        NSString *tempPath = NSTemporaryDirectory();
        NSString *filePath = [tempPath stringByAppendingPathComponent:@"person.data"];
        // archiveRootObject这个方法底层会去调用保存对象的encodeWithCoder方法,去询问要保存这个对象的哪些属性.
       // 所以要实现encodeWithCoder方法, 告诉要保存这个对象的哪些属性.
        [NSKeyedArchiver archiveRootObject:person toFile:filePath];
    }
    
    // 读取数据
    - (void)read {
        // 获取沙盒临时目录
        NSString *tempPath = NSTemporaryDirectory();
        NSString *filePath = [tempPath stringByAppendingPathComponent:@"person.data"];
        // NSKeyedUnarchiver会调用initWithCoder这个方法,来让你告诉它去获取这个对象的哪些属性.
        // 所以我们在保存的对象当中实现initWithCoder方法.
        Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"name=%@---age=%d",person.name,person.age);
    }
    
    // 要保存的对象
    #import <Foundation/Foundation.h>
    // 要遵守<NSCoding>协议
    @interface Person : NSObject<NSCoding>
    @property (nonatomic, strong) NSString *name;
    @property(nonatomic, assign) int age;
    @end
    
    @implement Person
    // archiveRootObject这个方法底层会去调用保存对象的encodeWithCoder方法,去询问要保存这个对象的哪些属性.
    // 只有遵守了NSCoding协议之后才能够实现这个方法.
    -(void)encodeWithCoder:(NSCoder *)encode{
        [encode encodeObject:self.name forKey:@"name"];
        [encode encodeInt32:self.age forKey:@"age"];
    }
    
    // NSKeyedUnarchiver会调用initWithCoder这个方法,来让你告诉它去获取这个对象的哪些属性.
    // initWithCoder什么时候调用:解析一个文件的时候就会调用.
    -(instancetype)initWithCoder:(NSCoder *)decoder{
         // 这个地方为什么没有[super initWithCoder]?
         // 是因为它的父类没有遵守NSCoding协议
        if (self = [super init]) {
          // 要给它里面的属性进行赋值,外界取得对象时访问该属性,里面才会用值.
           self.age = [decoder decodeInt32ForKey:@"age"];
           self.name = [decoder decodeObjectForKey:@"name"];
        }
        return self;
    }
    @end
    
  • 相关阅读:
    AIMS 2013中的性能报告工具不能运行的解决办法
    读懂AIMS 2013中的性能分析报告
    在线研讨会网络视频讲座 方案设计利器Autodesk Infrastructure Modeler 2013
    Using New Profiling API to Analyze Performance of AIMS 2013
    Map 3D 2013 新功能和新API WebCast视频下载
    为Autodesk Infrastructure Map Server(AIMS) Mobile Viewer创建自定义控件
    ADN新开了云计算Cloud和移动计算Mobile相关技术的博客
    JavaScript修改css样式style
    文本编辑神器awk
    jquery 开发总结1
  • 原文地址:https://www.cnblogs.com/zhenzhen123/p/5292413.html
Copyright © 2011-2022 走看看