iOS 数据持久化之覆盖式存储(Plist ,Preference,归档)

一,Plist存储方式

     1,获得存储目录:  

NSString *pathForDocument = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

     2,生成Plist文件并将想要存储的数组保存到Plist文件中:

NSString *fileName = [pathForDocument stringByAppendingPathComponent:@"123.plist"];
NSArray *arrayN = @[@"123",@"456",@"789"];
[arrayN writeToFile:fileName atomically:YES];

     3,读取之前保存的Plist文件:

NSArray *arrayResult = [NSArray arrayWithContentsOfFile:fileName];
NSLog(@"ReadPlist:%@",arrayResult);

二,Preference(偏好设置)存储方式:

      1,设置路径并保存需要存储的数据:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"Austin.Kuture" forKey:@"Name"];
[userDefaults setBool:YES forKey:@"Sex"];
[userDefaults setInteger:18 forKey:@"Age"];  
[userDefaults synchronize];//立即存储

      2,读取偏好设置中的数据:

NSString *name = [userDefaults objectForKey:@"Name"];
BOOL sex = [userDefaults boolForKey:@"Sex"];
NSInteger age = [userDefaults integerForKey:@"Age"]; 
NSLog(@"Name:%@  Sex:%d  Age:%ld",name,sex,(long)age);

三,归档存储方式:

      1,遵守NSCoding协议,新建person类:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Person : NSObject<NSCoding>

@property (nonatomic,strong) UIImage *img;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;

@end

     2,实现编码与解码方法,在person.m文件中:

#import "Person.h"

@implementation Person

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if ([super init]){
        
        self.name = [aDecoder decodeObjectForKey:@"Name"];
        self.img = [aDecoder decodeObjectForKey:@"Image"];
        self.age = [aDecoder decodeIntegerForKey:@"Age"];
    }
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.img forKey:@"Image"];
    [aCoder encodeObject:self.name forKey:@"Name"];
    [aCoder encodeInteger:self.age forKey:@"Age" ];
}//如果需要归档的类是某个自定义类的子类时,就需要在归档和解档之前先实现父类的归档和解档方法。即 [super encodeWithCoder:aCoder] 和 [super initWithCoder:aDecoder] 方法

      3,对想要存储数据或对象的类中导入头文件:

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (nonatomic,strong) UIImage *imgVC;
@property (nonatomic,copy) NSString *nameVC;
@property (nonatomic,assign) NSInteger ageVC;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    //[self cacheForKeyAchiver];
    //[self cacheForKeyUnAchiver];
    
}

      4,对数据或对象进行归档:

- (void)cacheForKeyAchiver{
    
    UIImage *imgVC = [UIImage imageNamed:@"Kuture"];
    NSString *nameVC = [NSString stringWithFormat:@"Austin.Kuture"];
    NSInteger ageVC = 18;
    
    self.imgVC = imgVC;
    self.nameVC = nameVC;
    self.ageVC = ageVC;
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Person.data"];
    Person *person = [[Person alloc]init];
    person.img = self.imgVC;
    person.age = self.ageVC;
    person.name = self.nameVC;
    
    [NSKeyedArchiver archiveRootObject:person toFile:file];
    
}

      5,在需要该数据或对象的类中进行解档:

- (void)cacheForKeyUnAchiver{
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Person.data"];
    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
    if (person){
        
        NSLog(@"NewImage:%@  NewName:%@ NewAge:%ld",person.img,person.name,person.age);
    }
    
}

 

 

 

 

 

 

 

 

    原文作者:移动开发
    原文地址: https://my.oschina.net/Kuture/blog/693033
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞