2015-09-17 52 views
3

我正在創建一個筆記應用程序,我試圖將數據保存到plist字典中,但是,當我保存新筆記的代碼將替換舊筆記時。swift - 保存plist字典而不覆蓋密鑰

如何在字典中添加新數據而不替換舊數據?

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray 
      let documentsDirectory = paths.objectAtIndex(0) as! NSString 
      let path = documentsDirectory.stringByAppendingPathComponent("notes.plist") 
      var dict: NSMutableDictionary = ["XInitializerItem": "DoNotEverChangeMe"] 
      //saving 

      dict.setObject(noteResult.text, forKey: nameSave.text) 

      //writing 

      dict.writeToFile(path, atomically: false) 
      let resultDictionary = NSMutableDictionary(contentsOfFile: path) 
      println("Saved note.plist file is --> \(resultDictionary?.description)") 

加載註釋:

func loadNotes(){ 

     let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray 
     let DocumentsDirectory = plistPath[0] as! String 
     let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist") 
     let fileManager = NSFileManager.defaultManager() 

     if (!fileManager.fileExistsAtPath(path)) { 

      if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") { 

       let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath) 
       println("Bundle notes.plist file is --> \(resultDictionary?.description)") 
       fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil) 
       println("copy") 

      } else { 
       println("notes.plist not found") 
      } 


     }else { 
      println("note.plist already exists") 

      //fileManager.removeItemAtPath(path, error: nil) 
     } 

     let resultDictionary = NSMutableDictionary(contentsOfFile: path) 
     println("Loaded notes.plist file is --> \(resultDictionary?.description)") 
     var myDict = NSDictionary(contentsOfFile: path) 

     if let dict = myDict { 
      //load values 

     } else { 

      println("worning ccould not create dictionary from notes.plist, default values will be used") 
     } 

    } 

enter image description here

回答

1

點1:可否請你把一個破發點,並從文件系統加載字典後仔細檢查,它包含的是數據先前保存。這很可能是真的,然後點2會幫助你。如果這一點是打破,那麼請確保解決這個問題,你會破解它:)。

第2點:詞典必然有唯一的鍵。問題在於你的下面的陳述。確保每次保存新筆記時,都會使用新筆記保存。每當您將數據保存在字典中時,請檢查 nameSave.text值。

dict[nameSave.text] = noteResult.text 

編輯:

我看到這裏的問題是,你是不是在文件系統中已保存的文件初始化你的字典。您必須首先確保在文件系統中使用notes.plist填充字典,然後向其添加新數據並最終將其保存回來。

這是我如何做到這一點;只是在流程中結合了你提到的兩種方法。在保存操作的觸發下調用此功能。在這裏,我想,第一次檢查可能會丟失。我沒有測試這個代碼,所以請耐心等待。

func saveNotes() { 
    // First fetch old notes 
    let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray 
    let DocumentsDirectory = plistPath[0] as! String 
    let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist") 
    let fileManager = NSFileManager.defaultManager() 

    if (!fileManager.fileExistsAtPath(path)) { 

     if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") { 

      let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath) 
      println("Bundle notes.plist file is --> \(resultDictionary?.description)") 
      fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil) 
      println("copy") 

     } else { 
      println("notes.plist not found") 
     } 


    } else { 
     println("note.plist already exists") 

     //fileManager.removeItemAtPath(path, error: nil) 
    } 

    let resultDictionary = NSMutableDictionary(contentsOfFile: path) 
    println("Loaded notes.plist file is --> \(resultDictionary?.description)") 
    var myDict = NSDictionary(contentsOfFile: path) 

    if let dict = myDict { 
     // Save new value 
     dict.setObject(noteResult.text, forKey: nameSave.text) 
    } else { 
     println("worning ccould not create dictionary from notes.plist, default values will be used") 
    } 

    // Now save it back 
    dict.writeToFile(path, atomically: false) 

} 
+0

謝謝你,關鍵的常是保存不同的值。 nameSave.text是一個UITextField,用戶爲要保存的鍵選擇一個值 – SNos

+0

好的。那麼第一點呢,你檢查了嗎? – Abhinav

+0

不知道在哪裏以及如何放置斷點 – SNos

0

以供將來參考的解決辦法是:

// First fetch old notes 
      let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray 
      let DocumentsDirectory = plistPath[0] as! String 
      let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist") 
      let fileManager = NSFileManager.defaultManager() 

      if (!fileManager.fileExistsAtPath(path)) { 

       if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") { 

        let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath) 
        println("Bundle notes.plist file is --> \(resultDictionary?.description)") 
        fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil) 
        println("copy") 

       } else { 
        println("notes.plist not found") 
       } 


      } else { 
       println("note.plist already exists") 

       //fileManager.removeItemAtPath(path, error: nil) 
      } 

      var myDict = NSDictionary(contentsOfFile: path) 

      if let dict = myDict { 
       // Save new value 
       var noteResultAll = "Date: " + saveDate.text! + "\n" + noteResult.text 

       dict.setValue(noteResultAll, forKey: nameSave.text) 
       dict.writeToFile(path, atomically: false) 
      } else { 
       println("worning ccould not create dictionary from notes.plist, default values will be used") 
      } 

      // Now save it back 

      let resultDictionary = NSMutableDictionary(contentsOfFile: path) 
      println("Loaded notes.plist file is --> \(resultDictionary?.description)")