简体   繁体   中英

Storing a dictionary in NSUserDefaults and using the data stored.

I have been struggling with this problem for 5 days. No joke. Please help! Here is the problem that I am encountering.

What I know:

I declared var addressDict = Dictionary<Int, Array<String>>() as a global variable. Now, I understand the following code is used to append the data stored in my addressDict and changing the type so that the data can be stored into NSUserDefaults.

let data = NSKeyedArchiver.archivedDataWithRootObject(addressDict) //archiving
self.defaults.setObject(data, forKey: "addressDict") //storing
if let data2 = self.defaults.objectForKey("addressDict") as? NSData { 
     let addressDict2 = NSKeyedUnarchiver.unarchiveObjectWithData(data2)
}

Now, every time I run the app, the data that is stored in addressDict2 gets overwritten by my addressDict because when the app starts, there is no data in addressDict . So even though I might have appended some data into addressDict2 , it is getting overwritten by addressDict which has no data.

So in order to solve this problem I can run the code above but without the line, self.defaults.setObject(data, forKey: "addressDict") , and now, the addressDict2 is no longer being overwritten by a variable that holds no data, addressDict . So the data I appended is stored in addressDict2 . However, if I do this, I can no longer append new information into addressDict2 because I don't have self.defaults.setObject(data, forKey: "addressDict") any more. I hope you see my dilemma here...

What I want to know:

This is what I would like my code to do. I want to be able to append new information into some variable, say addressDict2 . And when the app starts, I want the data that is in addressDict2 to be copied into addressDict . So this will cause addressDict to hold the data that I want it to hold.

What I've tried:

In order to try to explain what I meant above, here is some code. Note, this code does not work , it's just to, kind of, clarify what I mean and to show if the following code would work, it would solve my problem, I think (please correct me if I am wrong). So in my viewDidLoad() , I would have something like the following:

override func viewDidLoad() {
    super.viewDidLoad()

    if NSUserDefaults.standardUserDefaults().objectForKey("addressDict") != nil {
        if let data2 = defaults.objectForKey("addressDict") as? NSData { 
            addressDict = NSKeyedUnarchiver.unarchiveObjectWithData(data2) //there is an error here that states: "Cannot assign a value of type AnyObject? to a value of type Dictionary<Int, Array<Strings>>
        }
    }
}

So now, when I run this code:

let data = NSKeyedArchiver.archivedDataWithRootObject(addressDict) //archiving
self.defaults.setObject(data, forKey: "addressDict") //storing
if let data2 = self.defaults.objectForKey("addressDict") as? NSData { 
     let addressDict2 = NSKeyedUnarchiver.unarchiveObjectWithData(data2)
}

The addressDict here will no longer be empty and will not overwrite addressDict2 with empty data. And I will be able to use addressDict with all the permanently stored data! And life will be all good. But I have no idea how to do this.

PS I seriously apologize for the length of the question. Thank you for your time!

while saving data into dictionary:

let foundationDictionary = NSMutableDictionary(dictionary: StoreDictionary) let dict = NSKeyedArchiver.archivedDataWithRootObject(foundationDictionary) NSUserDefaults.standardUserDefaults().setObject(dict, forKey: "yourkey")

while using saved data:

if NSUserDefaults.standardUserDefaults().objectForKey("UserDetails")!=nil {

let dict : NSData = NSUserDefaults.standardUserDefaults().objectForKey("yourkey") as! NSData dictionaryName = NSKeyedUnarchiver.unarchiveObjectWithData(dict) as? NSDictionary
}

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