iOS 數據持久化之覆蓋式存儲(Plist ,Preference,歸檔)

一,Plist存儲方式數組

     1,得到存儲目錄:  編碼

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

     2,生成Plist文件並將想要存儲的數組保存到Plist文件中:atom

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

     3,讀取以前保存的Plist文件:code

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

二,Preference(偏好設置)存儲方式:orm

      1,設置路徑並保存須要存儲的數據:對象

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

      2,讀取偏好設置中的數據:string

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);

三,歸檔存儲方式:it

      1,遵照NSCoding協議,新建person類:io

#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文件中:class

#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);
    }
    
}
相關文章
相關標籤/搜索