檔案資料持久化

2022-09-11 23:42:25 字數 1168 閱讀 5205

概論

所謂的持久化,就是將資料儲存到硬碟中,使得在應用程式或機器重啟後可以繼續訪問之前儲存的資料。在ios開發中,有很多資料持久化的方案,接下來我將嘗試著介紹一下5種方案:

ios程式預設情況下只能訪問程式自己的目錄,這個目錄被稱為「沙盒」。沙盒的目錄結構如下:

應用程式包   nsstring *path = [[nsbundle mainbundle] bundlepath];

在沙盒中有幾個這樣的資料夾:(這些檔案都是位於home路徑下的)

(1)documents: 該資料夾用於程式資料檔案寫入到該目錄下,itunes同步該應用時會同步此資料夾中的內容,用於儲存使用者資料以及需要備份的資料。

(2)library:include caches and preferences .

preferences: itunes同步,該應用時會同步此資料夾中的內容,用於放置使用者的偏好資料,你不能直接建立偏好的設定檔案,應該使用nsuserdefaults的類來獲取和設定應用程式偏好

caches:itunes不同步,用於存放應用程式專用的支援檔案,儲存應用程式再次啟動過程需要的資訊。

(3)tmp:tunes不同步,臨時資料夾,下次啟動就會將該資料夾中的東西刪除。

獲取目錄的方法:

(1)獲取home目錄的方法:

nsstring *homedir = nshomedirectory();

(2)獲取documents目錄路徑的方法:

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

nsstring *docdir = [paths objectatindex:0];

(3)獲取caches目錄路徑的方法:

nsarray *paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);

nsstring *cachesdir = [paths objectatindex:0];

(4)獲取tmp目錄路徑的方法:

nsstring *tmpdir =  nstemporarydirectory();

資料持久化:

持久化 檔案

一 檔案 長久儲存資訊的一種資料資訊集合 開啟檔案,用寫的方式 r表示後面字串內容不需要轉義 f稱之為檔案控制代碼 f open r test01.txt w 檔案開啟後必須關閉 f.close 1.with語句開啟檔案 用with語句開啟檔案,不需要關閉 with open r test01.tx...

IOS 檔案讀寫 資料持久化

資料持久化的本質 將資料讀成檔案儲存在本地 沙盒機制 就是系統針對每個應用程式在本地生成的資料夾 名字隨機生成 對於不同的應用程式 不能訪問其他應用程式沙盒的內容 起到保護作用 1 documents 用來儲存長久儲存的資料 b perferences 儲存使用者的偏好設定 比如程式是否是第一次啟動...

資料持久化之 plist檔案

檔案目錄簡單說明 下面是利用字典將資料寫入到.plist檔案 cpp view plain copy print?viewcontroller.m plist created by rio.king on 13 9 22.import viewcontroller.h inte ce viewcon...