Swift从plist中取词典

时间:2014-12-03 14:15:01

标签: swift ios8

我将我的按钮保存在一个文件plist中

<dict>
<key>NameButton</key>
<array>
    <string>Button 1</string>
    <string>Button 2</string>
    <string>Button 3</string>
    <string>Button 4</string>
    <string>Button 5</string>
</array>
<key>NumberButton</key>
<string>5</string>
<key>button</key>
<dict>
    <key>Button 1</key>
    <string>0</string>
    <key>Button 2</key>
    <string>0</string>
    <key>Button 4</key>
    <string>0</string>
    <key>Button 5</key>
    <string>0</string>
    <key>Button 6</key>
    <string>0</string>
</dict>

我可以在此模式下从plist获取NameButton

 var Name:[String] = data!.valueForKey("NameButton") as [String]

如果我想从plist获取字典按钮? 例如,如果我想更改Button 4的值?

1 个答案:

答案 0 :(得分:0)

试试这个

    if var d = data {
        //get 'button' dictionary
        var button = d["button"] as? Dictionary<String, AnyObject>
        if var b = button {
            //get 'Button 4' value
            var button4 = b["Button 4"] as? String
            println(button4)
            if let b4 = button4 {
                //change 'Button 4' value
                b["Button 4"] = "5"
                println(b["Button 4"])
            }
        }
    }