資料儲存 歸檔解檔

2021-07-15 04:46:42 字數 825 閱讀 9202

#pragma mark - 實現協議方法

/** * 1.在"歸檔"的時候呼叫

* "歸檔":指的就是把物件寫入到檔案的過程

* 告訴系統,如何對當前物件進行歸檔(寫入檔案)

*/- (void)encodewithcoder:(nscoder *)encoder

/** * 2.在"反歸檔"的時候呼叫

* "反歸檔":指的是從檔案中讀取物件的過程

* 如何將讀取到的檔案中的資料轉換為乙個物件

*/- (instancetype)initwithcoder:(nscoder *)decoder

return

self;

}

// 1.建立自定義物件

czperson *p1 = [[czperson alloc] init];

p1.name = @"jack";

p1.age = 20;

p1.gender = 1;

// 2.指定要歸檔的檔案

nsstring *filepath = [self filepathwithfilename:@"gd.abc"];

// 1.指定要解檔的檔案

nsstring *filepath = [self filepathwithfilename:@"gd.abc"];

// 2.通過解檔讀取自定義物件

czperson *person = [nskeyedunarchiver unarchiveobjectwithfile:filepath];

swift 本地歸檔 解檔儲存

1.對使用者的模型資料 自定義類 hcusermodel 進行歸檔和解檔 1.1 需要遵循nscoding協議 1.2 需要實現func encode with acoder nscoder 歸檔方法 1.3需要實現 required init coder adecoder nscoder 解檔方法...

iOS 歸檔解檔

開發過程中有時需要儲存一些輕量級的資料,對於ios提供的幾種資料儲存方式在這時最合適當屬物件歸檔 nscoding 但是將物件資料進行歸檔解檔時候需要實現兩個方法 encodewithcoder和initwithencoder。encodewithcoder 編碼 initwithcoder 解碼使...

iOS 歸檔 解檔

1.新建類datacache 歸檔 void cachewithdata id data withfile nsstring file 解檔 id archivewithfile nsstring file 2.使用 儲存路徑 college mouth.arc 在虛擬機器上,簡單的寫可以找到路徑,...