• 归档


    ios数据存储之--对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)

    iOS/NSUserDefaults详解

     我要永远地记住你!(iOS中几种数据持久化方案)

    • plist文件(属性列表)
    • preference(偏好设置)
    • NSKeyedArchiver(归档)
    • SQLite 3
    • CoreData
    "应用程序包"    [[NSBundle mainBundle] bundlePath];
    Documents      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    Library    
        Caches     NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        Preferences  
    tmp             NSTemporaryDirectory();

    plist:

    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [path stringByAppendingPathComponent:@"123.plist"];
    
    NSArray *array = @[@"123", @"456", @"789"];
    [array writeToFile:fileName atomically:YES];
    
    NSArray *result = [NSArray arrayWithContentsOfFile:fileName];
    NSLog(@"%@", result);
    

    Preference:

    //1.获得NSUserDefaults文件
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    //2.向文件中写入内容
    [userDefaults setObject:@"AAA" forKey:@"a"];
    [userDefaults setBool:YES forKey:@"sex"];
    [userDefaults setInteger:21 forKey:@"age"];
    //2.1立即同步
    [userDefaults synchronize];
    
    //3.读取文件
    NSString *name = [userDefaults objectForKey:@"a"];
    BOOL sex = [userDefaults boolForKey:@"sex"];
    NSInteger age = [userDefaults integerForKey:@"age"];
    
    NSLog(@"%@, %d, %ld", name, sex, age);
    

    NSKeyedArchiver:

    遵循NSCoding协议

    /1.遵循NSCoding协议 
      @interface Person : NSObject <NSCoding>
    
      //2.设置属性
      @property (strong, nonatomic) UIImage *avatar;
      @property (copy, nonatomic) NSString *name;
      @property (assign, nonatomic) NSInteger age;
    
      @end
    //解档
      - (id)initWithCoder:(NSCoder *)aDecoder {
          if ([super init]) {
              self.avatar = [aDecoder decodeObjectForKey:@"avatar"];
              self.name = [aDecoder decodeObjectForKey:@"name"];
              self.age = [aDecoder decodeIntegerForKey:@"age"];
          }
    
          return self;
      }
    
      //归档
      - (void)encodeWithCoder:(NSCoder *)aCoder {
          [aCoder encodeObject:self.avatar forKey:@"avatar"];
          [aCoder encodeObject:self.name forKey:@"name"];
          [aCoder encodeInteger:self.age forKey:@"age"];
      }
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.data"];
    
      Person *person = [[Person alloc] init];
      person.avatar = self.avatarView.image;
      person.name = self.nameField.text;
      person.age = [self.ageField.text integerValue];
    
      [NSKeyedArchiver archiveRootObject:person toFile:file];
    
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.data"];
    
      Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
      if (person) {
         self.avatarView.image = person.avatar;
         self.nameField.text = person.name;
         self.ageField.text = [NSString stringWithFormat:@"%ld", person.age];
      }
    

    我要娶你做我的CoreData!

  • 相关阅读:
    Java基础之抽象类和多态
    Java基础之继承重载重写与this和super
    Java基础之StringBuffer的使用
    Java基础之String的方法与常量池
    Spring Boot之JSP开发
    Spring Boot之thymeleaf中替换th:each使用
    本周进度
    问卷调查立题报告(三人行)
    本周进度(复习软考)
    软件需求十步走之阅读笔记02
  • 原文地址:https://www.cnblogs.com/dqxu/p/6802512.html
Copyright © 2020-2023  润新知