如何将此数据写入我的Plist? - 斯威夫特

时间:2014-12-15 23:41:27

标签: ios swift save plist

我非常接近将新数据写回我的UserData.plist,但我在创建新数据字典方面存在问题,因此可以写出来。

func saveTriggerData(){

    //get current date
    let date = NSDate()

    //for each trigger, insert date,value
    for (index,trigger) in enumerate(triggersList){
        //if trigger already exists in userData, just add a new date, value
        if(triggersData[trigger] != nil){
            NSLog("Trigger " + trigger + " exists")
            //get value of corresponding stepper
            var value = Int(steppersList[index].value)
            //get previous data for this trigger

            //make a dictionary of new data
            var newData: [NSDate: NSNumber] = [date: value]
            triggersData.addEntriesFromDictionary(newData)

            //insert new data
            let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
            let documentsDirectory = paths.objectAtIndex(0) as NSString
            let path = NSBundle.mainBundle().pathForResource("UserData", ofType: "plist")
            //replace dict's Triggers value with our new Triggers
            NSLog("\(userData)")
            //userData.setObject(triggersData, forKey:"Triggers")
            //let fileWritten = userData.writeToFile(path!, atomically: true)
            //NSLog("Save trigger data: \(fileWritten)")

        }
        else{ //trigger does not yes exist in userData, add trigger name and then its date, value
           NSLog("Trigger " + trigger + " does not yet exist")
        }

    }

}

结果:

Triggers =     {
        "Caffeinated Drinks" =         (
                        (
                "2014-12-03 16:30:22 +0000",
                2
            ),
                        (
                "2014-12-04 16:30:02 +0000",
                1
            )
        );
        "Alcoholic Drinks" =         (
                        (
                "2014-12-03 16:28:53 +0000",
                3
            ),
                        (
                "2014-12-04 16:29:33 +0000",
                0
            )
        );
        "2014-12-15 23:36:53 +0000" = 0;
    };
}

最后一行" 2014-12-15 23:36:53 + 0000" = 0; 应该是酒精饮料的孩子。所以,我已经接近了,但我还没到那里,而且我对它疯狂了。任何帮助将不胜感激。

哦,和plist:

<key>Triggers</key>
    <dict>
        <key>Alcoholic Drinks</key>
        <array>
            <array>
                <date>2014-12-03T16:28:53Z</date>
                <integer>3</integer>
            </array>
            <array>
                <date>2014-12-04T16:29:33Z</date>
                <integer>0</integer>
            </array>
        </array>
        <key>Caffeinated Drinks</key>
        <array>
            <array>
                <date>2014-12-03T16:30:22Z</date>
                <integer>2</integer>
            </array>
            <array>
                <date>2014-12-04T16:30:02Z</date>
                <integer>1</integer>
            </array>
        </array>
    </dict>

1 个答案:

答案 0 :(得分:0)

您的triggersData是一个包含数组数组的字典。您当前正在添加newData(这是一个键入日期的数字字典)。您需要从datevalue创建一个数组,并将该数组附加到字典中的相关项目。所以,而不是:

var newData: [NSDate: NSNumber] = [date: value]
triggersData.addEntriesFromDictionary(newData)

尝试:

var newData : [AnyObject] = [date, value]
if var triggerArray = triggersData[trigger] as? [AnyObject] {
    triggerArray.append(newData)
    triggersData[trigger] = triggerArray
}

这将使数据与您的plist形成相同的形式。但如果我是你,我会重构你的plist - 最低级别的数组必须是[AnyObject],因为第一项是NSDate类型,而第二项是NSNumber类型,这是一个坏主意。