iOS 数据存储

作者: azhang_coder | 来源:发表于2016-11-30 01:12 被阅读0次

    1.plist文件存储

    • iOS中手机应用数据存储是保存在手机里的应用沙盒中的
    • plist文件存储一般都是存取字典和数组,直接写成plist文件,把它存到应用沙盒当中.
    • 只有在iOS当中才有plist存储,它是ios特有的存储方式.
    存数据
    - (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];
       
    }
    

    2.偏好设置存储

    • 使用偏好设置存储会将数据存储到应用沙盒的Preferences文件下,路径为
      ~~Application/0A46AC9F-04C9-4DA2-B803-5018A979109A/Library/Preferences
    • 一般用于存储应用的一些应用设置信息,用户偏好设置
      // 保存用户信息至沙盒
      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];
        
    

    3.归档

    • 归档一般用于保存自定义的对象
        //获取沙盒临时目录
        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<NSCoding>
    // 编码
    - (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];
    

    相关文章

      网友评论

        本文标题:iOS 数据存储

        本文链接:https://www.haomeiwen.com/subject/labzpttx.html