如何保存和检索字典数组的数组

时间:2014-11-21 09:53:46

标签: arrays dictionary swift plist

我有一个应用程序围绕的字典数组,需要持久化。 我无法将其保存到NSUserDefaults。 我可能会使用CoreData,如果我有使用它的经验,但它似乎有点矫枉过正,我只需要在发布时收集一次数据并在关闭时保存一次。

所以我试图找出如何在保存时将其写入plist文件(如果可能)。 并在应用程序启动时检索它。

数据:

// Declare var
var recordingsDictionaryArray = [[:]]
// Populate many times during app use
recordingsDictionaryArray.insert(["name":saveRecordingField.stringValue, "date":NSDate(), "path":recorder.url], atIndex: 0)

因此字典包含String,NSDate和NSUrl

的混合

这可以保存到我的applicationSupport文件夹中的plist文件中吗? 希望在发布时保存和重建它不会分开它。

我正在尝试一个例子,但如果我没有将[0]引用到数据变量中,我根本无法工作。如果我只是说var data = recordingsDictionaryArray我得到错误:

func applicationWillTerminate(aNotification: NSNotification) {
    // add some dummy content to the empty recordingsDictionaryArray
    recordingsDictionaryArray.insert(["name":"Recording One", "date":NSDate(), "path":"dummy path"], atIndex: 0) // dummy content
    recordingsDictionaryArray.insert(["name":"Recording Two", "date":NSDate(), "path":"another dummy path"], atIndex: 0) // dummy content
    recordingsDictionaryArray.insert(["name":"Recording Three", "date":NSDate(), "path":"final dummy path"], atIndex: 0) // dummy content

    // check the count
    var thecount = recordingsDictionaryArray.count
    println(thecount)



    var data = recordingsDictionaryArray[0] // if i do not add reference to item [0] data errors below
    var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    var path = paths.stringByAppendingPathComponent("data.plist")
    var fileManager = NSFileManager.defaultManager()
    if (!(fileManager.fileExistsAtPath(path)))
    {
        var bundle: NSString = NSBundle.mainBundle().pathForResource("recordings", ofType: "plist")!
        fileManager.copyItemAtPath(bundle, toPath: path, error:nil)
    }
    data.objectForKey(data)
    data.writeToFile(path, atomically: true)
    // I only get "Recording 3" saved to the plist
}

下面的答案1: 我觉得我已经接近与NSUserDefaults达成协议了,但是不知道如何将mutableRetrievedDictionary恢复到recordingsDictionaryArray之前,就像保存之前一样。

    // save
    NSUserDefaults.standardUserDefaults().setObject(recordingsDictionaryArray, forKey: "recordingsArray")
    NSUserDefaults.standardUserDefaults().synchronize()



    // retrieve
    var mutableRetrievedDictionary: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("recordingsArray")?.mutableCopy()

2 个答案:

答案 0 :(得分:0)

您绝对可以将此保存到NSUserDefaults。 试试这个:

let standard = NSUserDefaults.standardUserDefaults() //Helps when you call it a lot
var recordingsDictionaryArray : NSMutableArray!

//SAVE
standard.setObject("anyObject", forKey: "myKey")
standard.synchronize()

//RETRIEVE
recordingsDictionaryArray = standard.objectForKey("myKey") as NSMutableArray
//Force the object called back to be mutable again

答案 1 :(得分:0)

经过大量半受过教育的试验和错误后,我找到了一个解决方案,在这种情况下,对我来说,使用NSUserDefaults:

func applicationWillTerminate(aNotification: NSNotification) {
    // add some dummy content to the empty recordingsDictionaryArray
    recordingsDictionaryArray.insert(["name":"Recording One", "date":NSDate(), "path":"dummy path"], atIndex: 0) // dummy content
    recordingsDictionaryArray.insert(["name":"Recording Two", "date":NSDate(), "path":"another dummy path"], atIndex: 0) // dummy content
    recordingsDictionaryArray.insert(["name":"Recording Three", "date":NSDate(), "path":"final dummy path"], atIndex: 0) // dummy content


    // Save to NSUserDefaults
    NSUserDefaults.standardUserDefaults().setObject(recordingsDictionaryArray, forKey: "recordingsArray")
    NSUserDefaults.standardUserDefaults().synchronize()

获取数据

func applicationDidFinishLaunching(aNotification: NSNotification) {

    recordingsDictionaryArray = NSUserDefaults.standardUserDefaults().objectForKey("recordingsArray")?.mutableCopy() as Array