Swift數(shù)據(jù)儲(chǔ)存方式:不同目錄的使用、Plist存儲(chǔ)數(shù)據(jù)、UserDefaults存儲(chǔ)數(shù)據(jù)

本文主要內(nèi)容

  1. iOS沙盒的目錄結(jié)構(gòu)
  2. 使用plist文件存儲(chǔ)
  3. 使用NSUserDefaults存儲(chǔ)數(shù)據(jù)

目錄結(jié)構(gòu)

1. Home目錄。

每個(gè)應(yīng)用程序都有對(duì)應(yīng)的私有目錄,其根目錄為Home目錄。該目錄下又三個(gè)文件夾:Documents、Library、tmp。

iOS目錄結(jié)構(gòu).png

2.Documents/Library/tmp目錄使用對(duì)比

每個(gè)目錄都有特定的使用方式和起行為特點(diǎn),根據(jù)不同的需要進(jìn)行數(shù)據(jù)的存放。


不同目錄的對(duì)比

3.應(yīng)用升級(jí)時(shí)的數(shù)據(jù)處理

應(yīng)用更新時(shí),涉及原應(yīng)用目錄文件的遷移,這也是大家比較關(guān)心的。扒了下網(wǎng)絡(luò)上過(guò)關(guān)于iOS應(yīng)用升級(jí)時(shí)的文件數(shù)據(jù)處理過(guò)程,原文如下:

Files Saved During Application Updates When a user downloads an application update, iTunes installs the update in a new application directory. It then moves the user’s data files from the old installation over to the new application directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:

  • Application_Home/Documents_
  • Application_Home/Library_
    Although files in other user directories may also be moved over, you should not rely on them being present after an update.

粗略翻譯下:
當(dāng)用戶下在應(yīng)用更新版本是,iTunes會(huì)在一個(gè)新應(yīng)用目錄下安裝升級(jí)版,之后會(huì)把用戶數(shù)據(jù)從舊應(yīng)用移動(dòng)新應(yīng)用目錄下,完成后才刪除舊應(yīng)用。以下文件目錄在應(yīng)用升級(jí)時(shí)確保會(huì)保存下來(lái):1.Documents目錄。2.Library目錄。雖然其他文件目錄下的文件可能也會(huì)遷移過(guò)去,但并不能指望升級(jí)后一定能展示這些數(shù)據(jù)。

4.各目錄獲?。ùa)

//Home目錄獲取方式
let homePath = NSHomeDirectory( )

//Documents目錄獲取方式
let doucumentPath = NSHomeDirectory( ) + "/Documents"

//Library目錄獲取方式
let libraryPath2 = NSHomeDirectory() + "/Library"

//Preferences目錄一般不直接使用,而是用NSUserDefaults來(lái)創(chuàng)建偏好文件

//Caches目錄獲取方式
let cachePath = NSHomeDirectory() + "/Library/Caches"

//tmp目錄的獲取方式
let tmpDirectory = NSHomeDirectory() + "/tmp"

關(guān)于目錄獲取,我自己寫(xiě)了個(gè)的擴(kuò)展,只要在需要獲取目錄路徑的類中遵循SaveDataDoucumentPath協(xié)議就可以用擴(kuò)展實(shí)現(xiàn)中的方法快速獲取對(duì)應(yīng)路徑。

protocol SaveDataDocumentPath {
}

extension SaveDataDocumentPath {
    func HomePath() -> String {
        return NSHomeDirectory()
    }
    func DocumentsPath() -> String {
        return HomePath() + "/Documents"
    }
    func LibraryPath() -> String {
        return HomePath() + "/Library"
    }
    func tmpPath() -> String {
        return HomePath() + "/tmp"
    }
    func PreferencesPath() -> String {
        return LibraryPath() + "/Preferences"
    }
    func CachesPath() -> String {
        return LibraryPath() + "/Caches"
    }
}

plist文件的使用

  • 可以自己創(chuàng)建.plist文件進(jìn)行數(shù)據(jù)的讀寫(xiě)
  • .plist文件實(shí)際上是,一個(gè)XML格式的文件
  • 支持的數(shù)據(jù)類型有:NSDictionary、NSArray、Boolean、NSData、NSNumber、NSString。Swift3.0暫時(shí)不支持Array、Dictionary等類型,但支持String。建議都使用帶NS的類型。
  • plist文件存取方法分為3步:
    1. 根據(jù)需要獲取存儲(chǔ)路徑(如上面所說(shuō)的)
    2. 把數(shù)據(jù)寫(xiě)入對(duì)應(yīng)的plist文件
    3. 通過(guò)路徑地址讀取plist文件
      //獲取路徑,需要包括目標(biāo)文件名。如果目錄中沒(méi)有,系統(tǒng)會(huì)自行創(chuàng)建
      let filePath = NSHomeDirectory() + "/Documents/Number.plist"
      //數(shù)據(jù)寫(xiě)入plist文件
      let array = NSArray.init(objects:1, 2, 3)
      array.writeToFile(filePath, atomically:ture)
      //從plist讀取數(shù)據(jù)
      let array = NSArray(contentsOfFile: NSHomeDirectory( ) + "Documents/Number.plist")
  • 注意:對(duì)同一文件進(jìn)行重復(fù)寫(xiě)入,之前的數(shù)據(jù)會(huì)被覆蓋。
  • 實(shí)際上可以使用writeToFile方法寫(xiě)入任何格式的文件,其內(nèi)容本身是XML格式。
文件內(nèi)容

NSUserDefaults

  • 存儲(chǔ)目錄:Library/Preference

  • 實(shí)際也是通過(guò)plist文件存儲(chǔ)。

  • 每個(gè)應(yīng)用都有個(gè)NSUserDefaults實(shí)例,可以通過(guò)它進(jìn)行偏好的存取。

  • 可以存儲(chǔ)的數(shù)據(jù)類型:NSData、NSString、NSNumber、NSDate、NSArray、NSDictionary。

    //存儲(chǔ)基礎(chǔ)類型,以Int為例。其他雷同
    UserDefaults.standard.set(15, forKey:"theKey")
    
    //讀取基礎(chǔ)類型,以Int為例。其他雷同。
    let num = UserDefaults.standard.integer(forKey: "theKey")
    
  • UserDefault設(shè)置數(shù)據(jù)時(shí),不是馬上寫(xiě)入,而是根據(jù)時(shí)間戳定時(shí)把緩存數(shù)據(jù)寫(xiě)入磁盤(pán)。所以使用set設(shè)置數(shù)據(jù)后,為了方式數(shù)據(jù)丟失,應(yīng)該馬上調(diào)用synchornize方法強(qiáng)制把數(shù)據(jù)寫(xiě)入磁盤(pán)。
    userDefault.synchornize()

  • 對(duì)于不是基礎(chǔ)數(shù)據(jù)類型,可以轉(zhuǎn)換為NSData進(jìn)行存儲(chǔ)。
    //使用以下方法轉(zhuǎn)化為NSData類型
    let data:NSData = NSKeyedArchiver.archivedDataWithRootObject(image)
    //使用以下方法轉(zhuǎn)化為原來(lái)的類型
    let img = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! UIImage

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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