写一个数组到.plist文件Swift

时间:2015-06-24 22:14:55

标签: ios swift plist

我有一个功能:

func saveData (numbertext : String, codetext: String) {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentDir = paths.objectAtIndex(0) as! NSString
    let path = documentDir.stringByAppendingPathComponent("data.plist")
    var dict: NSMutableDictionary = ["name" : "data"]
    //save values
    dict.setObject(numbertext, forKey: data.numberKey)
    dict.setObject(codetext, forKey: data.codeKey)
    dict.writeToFile(path, atomically: false)
}

我想将该数组写入.plist文件:

var tarif : Array<String> = ["first","second","third"]

numbertextcodetext在另一个功能中。如何将它们写入数组?

1 个答案:

答案 0 :(得分:1)

要保存它,您可以使用

简单地传递它
dict.setObject (tarif, forKey:...)

并使用

检索它
tarif = dict.objectForKey(...) as! [String]