简体   繁体   English

在NSUserDefaults错误中存储Int和Array的字典

[英]Storing a dictionary of Int and Array in NSUserDefaults error

What I know so far about the subject: 到目前为止,我对该主题的了解:

I understand that there are only certain data types that you can store into NSUserDefaults: 我了解您只能将某些数据类型存储到NSUserDefaults中:

The NSUserDefaults class provides convenience methods for accessing common types such as floats , doubles , integers , Booleans , and URLs . NSUserDefaults类提供了方便的方法来访问常见类型,例如floatsdoublesintegersBooleansURLs A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. 默认对象必须是属性列表,即NSData,NSString,NSNumber,NSNumber,NSDate,NSArray或NSDictionary的一个实例(或用于集合的实例的组合)。 If you want to store any other type of object, you should typically archive it to create an instance of NSData. 如果要存储任何其他类型的对象,通常应将其归档以创建NSData实例。

And I know I can do the following: 我知道我可以执行以下操作:

var cities = [String]()
cities = NSUserDefaults.standardUserDefaults().objectForKey("cities") as! NSArray

And this is permissible because it casts my cities String array into an NSArray and NSUserDefaults accepts this data type. 这是允许的,因为它将我的城市String数组强制转换为NSArray,并且NSUserDefaults接受此数据类型。 However, I have a dictionary of an Int and an Array, namely, var addressDict = Dictionary<Int, Array<String>>() . 但是,我有一个Int和Array的字典,即var addressDict = Dictionary<Int, Array<String>>() I know that NSUserDefaults accepts NSDictionary, NSInteger, and NSArray. 我知道NSUserDefaults接受NSDictionary,NSInteger和NSArray。

What I want to know how to do: 我想知道怎么做:

I want to cast my addressDict properly so that NSUserDefaults accepts my addressDict. 我想正确转换我的addressDict ,以便NSUserDefaults接受我的addressDict。 Though, I know that the Dictionary, Array, and Int can be casted into it's NS counterparts as separate entities, but I don't know how to do that altogether as the data type Dictionary<Int, Array<String>>() . 虽然,我知道Dictionary,Array和Int可以作为单独的实体转换为NS对应对象,但是我不知道如何作为数据类型Dictionary<Int, Array<String>>()来完全做到这一点。 Any help? 有什么帮助吗? For example, I know this is completely wrong but something like NSDictionary<NSInteger, NSArray<String>> or something. 例如,我知道这是完全错误的,但是类似NSDictionary<NSInteger, NSArray<String>>东西。

To store a dictionary in NSUserDefaults you need to convert them to NSData first. 要将字典存储在NSUserDefaults您需要NSUserDefaults其转换为NSData NSKeyedArchiver and NSKeyedUnarchiver are perfect for the job. NSKeyedArchiverNSKeyedUnarchiver非常适合此工作。 The following example shows how to store your dictionary in NSUserDefaults and read it back. 以下示例显示如何将字典存储在NSUserDefaults并读回。

    var addressDict = Dictionary<Int, Array<String>>()
    addressDict[3] = ["string1", "string2"] //some example values
    let data = NSKeyedArchiver.archivedDataWithRootObject(addressDict) //archiving
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(data, forKey: "address") //storing
    if let data2 = defaults.objectForKey("address") as? NSData { //reading
        let addressDict2 = NSKeyedUnarchiver.unarchiveObjectWithData(data2) //unarchiving
        print(addressDict2)
    }

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

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