如何将数据保存到plist文件中的多维数组

时间:2015-05-31 11:51:01

标签: swift multidimensional-array plist

我想将一些配置数据保存到包含数组和常规变量类型的plist文件中。它看起来像这样:

http://s10.postimg.org/f2xpgur1l/plist.png) (抱歉,我无法附上任何图片)

我的功能是这样的:

// Saves the data into a plist file
func saveObject(i_fileName: String, i_keyForValueToSave: String, i_valueToSave: String, i_keyForXInitializer:String, i_valueXInitializer: String) {

    var fileName: String = i_fileName

    var keyForValueToSave: String = i_keyForValueToSave
    var valueToSave: AnyObject = i_valueToSave

    var keyForXInitializer: String = i_keyForXInitializer
    var valueXInitializer: String = i_valueXInitializer

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths.objectAtIndex(0) as! NSString
    let path = documentsDirectory.stringByAppendingPathComponent(fileName)

    var dict: NSMutableDictionary = [keyForXInitializer: valueXInitializer]

    //saving values
    dict.setObject(valueToSave, forKey: keyForValueToSave)

    //writing to plist
    dict.writeToFile(path, atomically: false)

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

我称之为:

saveObject("Selection.plist", "Item 0", "testConfig, "Item 3", "XInitializerItem_01")

结果看起来像是这样:

http://s13.postimg.org/kbq103707/result.png) (抱歉,我无法附上任何图片)

如您所见,应保存到数组中的“testConfig”将保存为pilst文件中的普通字符串。这不是我的目标。我想要添加到此数组的每个项目。我是SWIFT的新手,而且我是一个但却陷入太空的人。任何帮助都会很棒!谢谢!

2 个答案:

答案 0 :(得分:0)

您将valueToSave声明为AnyObject,但它保留i_valueToSave,在您的函数参数中声明为String

因此,当您执行dict.setObject(valueToSave, forKey: keyForValueToSave)时,会保存String

您应该将i_valueToSave函数参数的类型声明为AnyObject而不是String,然后在函数内部,您可以将i_valueToSave投射到所需的类型在保存之前。

假设i_valueToSave是一个字符串数组,那么:

if let valueToSave = i_valueToSave as? [String] {
    dict.setObject(valueToSave, forKey: keyForValueToSave)
}

如果是NSArray:

if let valueToSave = i_valueToSave as? NSArray {
    dict.setObject(valueToSave, forKey: keyForValueToSave)
}

答案 1 :(得分:0)

下面的代码会创建您需要的plist,您只需要正确填充值以满足您的需求

func saveObject(i_fileName: String, i_keyForValueToSave: String, i_valueToSave: String, i_keyForXInitializer:String, i_valueXInitializer: String) {

    var fileName: String = i_fileName
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths.objectAtIndex(0) as! NSString
    let path = documentsDirectory.stringByAppendingPathComponent(fileName)

    var dict = NSMutableDictionary()
    var item0 = [1,2,3]
    var item1 = [1,2,3,4]
    var item2 = false
    var item3 = "xInitializerItem_01"
    dict.setObject([item0, item1, item2, item3], forKey: "selection_01")
    item0 = [4,5,6]
    item1 = [5,6,7,8]
    item2 = true
    item3 = "xInitializerItem_02"
    dict.setObject([item0, item1, item2, item3], forKey: "selection_02")
    dict.writeToFile(path, atomically: true)

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

这可能是由上面的代码引起的:

enter image description here

您无法将值附加到plist,因此每次保存时都需要再次保存所有值