iOS 歸檔和解檔

  • iOS的幾種數(shù)據(jù)持久化方案

  • plist文件(屬性列表)

  • preference(偏好設(shè)置)

  • NSKeyedArchiver(歸檔)

  • SQLite

  • CoreData

  • 沙盒目錄結(jié)構(gòu)

  • "應(yīng)用程序包": 這里面存放的是應(yīng)用程序的源文件,包括資源文件和可執(zhí)行文件。

  • Documents: 最常用的目錄,iTunes同步該應(yīng)用時會同步此文件夾中的內(nèi)容,適合存儲重要數(shù)據(jù)。

  • Library/Caches: iTunes不會同步此文件夾,適合存儲體積大,不需要備份的非重要數(shù)據(jù)。

  • Library/Preferences: iTunes同步該應(yīng)用時會同步此文件夾中的內(nèi)容,通常保存應(yīng)用的設(shè)置信息。

  • tmp: iTunes不會同步此文件夾,系統(tǒng)可能在應(yīng)用沒運行時就刪除該目錄下的文件,所以此目錄適合保存應(yīng)用中的一些臨時文件,用完就刪除。

  • plist文件,plist文件是將某些特定的類,通過XML文件的方式保存在目錄中。

  • 這里先說說自定義類的歸檔和解檔
    NSObject沒有遵循<NSCoding>協(xié)議,所以想要調(diào)用- (instancetype)initWithCoder:(NSCoder *)aDecoder/- (void)encodeWithCoder:(NSCoder *)aCoder方法需要讓自定義類遵循該協(xié)議
    如果需要歸檔的類是某個自定義類的子類時,就需要在歸檔和解檔之前先實現(xiàn)父類的歸檔和解檔方法。即 [super encodeWithCoder:aCoder][super initWithCoder:aDecoder]方法;

  • 使用
    需要把對象歸檔是調(diào)用NSKeyedArchiver的工廠方法 archiveRootObject: toFile: 方法。
    需要從文件中解檔對象就調(diào)用NSKeyedUnarchiver的一個工廠方法 unarchiveObjectWithFile: 即可。

  • 代碼

#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCoding>

@property (nonatomic, copy) NSString *dream;

@end
#import "Person.h"

@implementation Person

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        NSLog(@"initWithCoder");
        _dream = [aDecoder decodeObjectForKey:@"DreamKey"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    NSLog(@"encodeWithCoder");
    [aCoder encodeObject:_dream forKey:@"DreamKey"];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end
#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (nonatomic, strong) Person *boy;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Person *p = [[Person alloc] init];
    self.boy = p;
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [btn1 setBackgroundColor:[UIColor blueColor]];
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 100, 100)];
    [btn2 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    
    [btn1 setTitle:@"歸檔" forState:UIControlStateNormal];
    [btn2 setTitle:@"解檔" forState:UIControlStateNormal];
    
    [btn1 addTarget:self action:@selector(clickBtn1) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(clickBtn2) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)clickBtn1{
    self.person.dream = @"happy";
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:self.person];
    
    NSString *homePath = NSHomeDirectory();
    NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches/hehe.archive"];
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

- (void)clickBtn2{
    
    NSString *homePath = NSHomeDirectory();
    NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches/hehe.archive"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arr.count) {
        self.person = arr[0];
        NSLog(@"%@", self.person.dream);
    }

}

@end

運行結(jié)果

歸檔和解檔
  1. 點擊解檔,啥都沒發(fā)生,
  2. 點擊歸檔,打印encodeWithCoder
  3. 退出App
  4. 點擊解檔,打印"initWithCoder" "happy"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容