简体   繁体   中英

Appending an Array in NSUserDefaults Swift

So I want to append an array in the NSUserDefaults, not save an entirely new one. The issue is that I want the array to be a logged history of a certain type of user interaction so I need to be appending an existing array in the NSUserDefaults, and not overwriting it.

ie Not:

var data = [String]()
var questionsAsked = [String]()

func storeData() {

    let defaults = NSUserDefaults.standardUserDefaults()

    defaults.setObject(questionsAsked, forKey: "questionsAsked")
    defaults.setObject(data, forKey: "data")

    var storedQuestionsAsked = defaults.objectForKey("questionsAsked") as? [String] ?? [String]()

    var storedData = defaults.objectForKey("data") as? [String] ?? [String]()

}

The array(s) are based in my Modal, and the appending is occurring by appending that array in the ViewController of that Modal

I tried just calling a function like this, but this is just calling an empty array because its never being re-saved to the NSUserDefaults:

func updateQuestionsAsked(questionAsked: String, answerGiven: String) {

    let defaults = NSUserDefaults.standardUserDefaults()

    defaults.setObject([String](), forKey: "questionsAsked")
    defaults.setObject([String](), forKey: "data")

    var storedQuestionsAsked = defaults.objectForKey("questionsAsked") as? [String] ?? [String]()
    var storedData = defaults.objectForKey("data") as? [String] ?? [String]()

    storedData.append(questionAsked)
    storedQuestionsAsked.append(answerGiven)

    NSUserDefaults.standardUserDefaults().synchronize()
}

Is there some kind of synchronization thing I'm missing? I'm sure this is super simple but I'm just spacing out on what to do. Thanks!

YOu are setting empty data for keys "questionsAsked" & "data",

func updateQuestionsAsked(questionAsked: String, answerGiven: String) {

    let defaults = NSUserDefaults.standardUserDefaults()

    //remove this
    //defaults.setObject([String](), forKey: "questionsAsked")
    //defaults.setObject([String](), forKey: "data")

    var storedQuestionsAsked = defaults.objectForKey("questionsAsked") as? [String] ?? [String]()
    var storedData = defaults.objectForKey("data") as? [String] ?? [String]()

    storedData.append(questionAsked)
    storedQuestionsAsked.append(answerGiven)

    // then update whats in the `NSUserDefault`
    defaults.setObject(questionsAsked, forKey: "questionsAsked")
    defaults.setObject(data, forKey: "data")

    // call this after you update
    defaults.synchronize()
}

Another is maybe you failed to set object here...

func storeData() {

    let defaults = NSUserDefaults.standardUserDefaults()

    defaults.setObject(questionsAsked, forKey: "questionsAsked")
    defaults.setObject(data, forKey: "data")

    // call this after setting Objects
    //--
    defaults.synchronize()
    //--

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM