初級資料持久化

2021-07-08 16:52:55 字數 3230 閱讀 5099

沙盒機制:

列印沙盒的各個資料夾路徑:

- (void)path

簡單物件的寫入:

// 系統的類例項出來的物件 叫簡單物件

// 例如: 字串 陣列 字典 二進位制物件(nsdata)

// 如果要寫入乙個陣列 或者 字典 等容器類物件

// 那麼這個容器當中 也要儲存是 簡單物件才能寫入

- (void)writefile

; [dic writetofile:dicpath atomically:yes];

nslog(@"%@",dicpath);

// 寫入data資料

nsstring *datastr = @"哈哈哈";

nsdata *data = [datastr datausingencoding:nsutf8stringencoding];

[data writetofile:datapath atomically:yes];

nslog(@"%@",datapath);

// data檔案一般是儲存的

// uiimagepngrepresentation 把轉化成data型別

nsdata *imagedata = uiimagepngrepresentation([uiimage imagenamed:@"1"]);

[imagedata writetofile:imagepath atomically:yes];

nslog(@"%@",imagepath);

}

讀取資料:

- (void)readfile

對資料夾進行建立 移動 複製 刪除操作:

// 建立資料夾方法

- (void)createfile

// 移動資料夾

- (void)movefile

// 拷貝資料夾

- (void)copyfile

// 刪除資料夾

- (void)deletefile

// 判斷資料夾是否存在

- (void)i***ecutable

資料的歸檔和反歸檔:

歸檔和反歸檔一般都用在複雜物件寫入檔案中

複雜物件寫入步驟:

遵守乙個協議 nscoding (需要在寫入的時候按照一定的規則進行寫入 一定的編碼格式去寫入) 歸檔與反歸檔(序列與反序列)

重寫(協議中)歸檔與反歸檔方法

建立乙個複雜物件出來

建立乙個歸檔物件

利用歸檔物件把複雜物件轉化成data

把data寫入檔案 進行資料持久化

複雜類的屬性歸檔與反歸檔:

// 歸檔方法

- (void)encodewithcoder:(nscoder *)acoder

// 反歸檔方法

- (instancetype)initwithcoder:(nscoder *)adecoder

return

self;

}

將複雜類進行歸檔:

// 複雜物件寫入檔案

// 自己建立出來的類(例如 student類)

// 寫入的核心思想:

// 把複雜物件轉化成 簡單物件 進行寫入

// 一般轉化成 nsdata物件進行寫入

student *student = [[student alloc] init];

student.name = @"王龍";

student.age = 18;

student.imagedata = uiimagepngrepresentation([uiimage imagenamed:@"1"]);

// 建立乙個可變data

nsmutabledata *data = [nsmutabledata data];

// 建立乙個歸檔物件

nskeyedarchiver *archiver = [[nskeyedarchiver alloc] initforwritingwithmutabledata:data];

// 將複雜物件 通過歸檔物件 轉化成data

[archiver encodeobject:student forkey:@"student"];

// 完成歸檔

[archiver finishencoding];

// 實際上 通過 歸檔物件 把 複雜物件 寫入data中

// 並且寫入的標識 就是key

// 歸檔 與 反歸檔 不是資料持久化

// 只是把複雜物件 按照系統的編碼格式 轉化成data

// 真正的資料持久化 還是 寫入檔案

// nslog(@"%@",data);

// 獲取路徑

nsstring *documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) firstobject];

// 拼接路徑

nslog(@"%@",path);

// 寫入檔案

[data writetofile:path atomically:yes];

// 釋放歸檔物件

[archiver release];

// 反歸檔

// 從沙盒中 讀取data資料

nsdata *datanew = [nsdata datawithcontentsoffile:path];

// 建立反歸檔物件

nskeyedunarchiver *unarchiver = [[nskeyedunarchiver alloc] initforreadingwithdata:datanew];

// 利用反歸檔物件 反歸檔出複雜物件

student *stunew = [unarchiver decodeobjectforkey:@"student"];

// 完成反歸檔

[unarchiver finishdecoding];

uiimage *image = [uiimage imagewithdata:stunew.imagedata];

// 釋放反歸檔物件

[unarchiver release];

初級資料的持久化

本章部落格我主要是把ui後期初級資料持久化的一些基本使用總結了下,這裡主要分享了沙盒機制的概念,簡單物件不同情況下的寫入,複雜物件的寫入,以及data與二進位制 之間相互轉化的方法,希望可以幫到大家。一 沙盒機制 1.每個應用程式位於檔案系統的嚴格限制部分 2.每個應用程式只能在為該程式建立的檔案系...

TwistedFate 初級資料持久化

沙盒機制 開啟沙盒的各個資料夾路徑 void path簡單物件的寫入 void writefile dic writetofile dicpath atomically yes 寫入data nsstring datastr 且隨疾風前行 nsdata data datastr datausinge...

18 初級資料持久化

分享 利用paros進行ios應用抓包。沙盒路徑 區別 bundle路徑下 唯讀不寫 sandbox路徑下 可讀可寫 讀取資料 方法initwithcontentsoffile 陣列和字典中的物件型別,必須是nsstring nsarray nsdata nsdictionary model型別不可...