5

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.

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

Wain
  • 118,658
  • 15
  • 128
  • 151
Jae
  • 321
  • 3
  • 12
  • I have already answered your question [here](http://stackoverflow.com/a/32264073/730701). The code I posted compiles and works fine. What you need here is a simple typecast. – Adam Sep 02 '15 at 13:14
  • Actually it's not needed to use `NSKeyedUnarchiver` because a dictionary containing `Int`, `String` and `Array` types is property list compliant. – vadian Sep 02 '15 at 13:31
  • @Adam, I'm not sure if you answered the question that I posed here (of course, I can very well be incorrect). I think I take it a step further by trying to, as @Rajatp, put it, merge `addressDict` and `addressDict2` so that the data in `addressDict2` can be "put into" `addressDict`. – aejhyun Sep 02 '15 at 13:46

1 Answers1

1

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
}
Erik Godard
  • 5,930
  • 6
  • 30
  • 33