简体   繁体   English

在NSUserDefaults中存储字典并使用存储的数据。

[英]Storing a dictionary in NSUserDefaults and using the data stored.

I have been struggling with this problem for 5 days. 我一直在努力解决这个问题5天。 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. 我将var addressDict = Dictionary<Int, Array<String>>()为全局变量。 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. 现在,我了解下面的代码用于附加存储在addressDict中的数据并更改类型,以便将数据存储到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 . 现在,每次运行应用程序时,存储在addressDict2的数据都会被我的addressDict覆盖,因为当应用程序启动时, addressDict没有数据。 So even though I might have appended some data into addressDict2 , it is getting overwritten by addressDict which has no data. 因此,即使我可能已经将一些数据附加到addressDict2 ,它也会被没有数据的addressDict覆盖。

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 . 所以为了解决这个问题,我可以运行上面的代码,但没有行, self.defaults.setObject(data, forKey: "addressDict") ,现在, addressDict2不再被不包含数据的变量覆盖, addressDict So the data I appended is stored in addressDict2 . 所以我追加的数据存储在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. 但是,如果我这样做,我就不能再将新信息附加到addressDict2因为我没有self.defaults.setObject(data, forKey: "addressDict")了。 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 . 我希望能够将新信息附加到某个变量中,比如addressDict2 And when the app starts, I want the data that is in addressDict2 to be copied into addressDict . 当应用程序启动时,我希望将addressDict2的数据复制到addressDict So this will cause addressDict to hold the data that I want it to hold. 所以这将导致addressDict保存我想要它保存的数据。

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: 所以在我viewDidLoad()我会下面这样

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. 这里的addressDict将不再为空,并且不会用空数据覆盖addressDict2 And I will be able to use addressDict with all the permanently stored data! 我将能够将addressDict与所有永久存储的数据一起使用! 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. PS我对这个问题的长度深表歉意。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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