存储Int的NSUserDefaults的中误差字典和数组 [英] Storing a dictionary of Int and Array in NSUserDefaults error

查看:109
本文介绍了存储Int的NSUserDefaults的中误差字典和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道到目前为止,关于主题是什么:

据我所知,有一些可以存储到仅NSUserDefaults的某些数据类型:

I understand that there are only certain data types that you can store into NSUserDefaults:

该NSUserDefaults的类提供了方便的方法来访问常见的类型,如浮动双打整数布尔网​​址。默认的对象必须是一个属性列表,也就是(或集合实例的组合)的一个实例:NSData的,的NSString,NSNumber的,NSDate的,NSArray的,或者NSDictionary的。如果你想存储的任何其他类型的对象,你通常应该归档它创建NSData的实例。

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. 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. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

和我知道我能做到以下几点:

And I know I can do the following:

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

这是允许的,因为它蒙上我的城市String数组到一个NSArray和接受NSUserDefaults的这种数据类型。不过,我有一个int和一个Array,即 VAR addressDict =词典℃的字典;诠释,阵列<弦乐>>()。我知道,接受NSUserDefaults的NSDictionary的,NSInteger的,和NSArray的。

And this is permissible because it casts my cities String array into an NSArray and NSUserDefaults accepts this data type. However, I have a dictionary of an Int and an Array, namely, var addressDict = Dictionary<Int, Array<String>>(). I know that NSUserDefaults accepts NSDictionary, NSInteger, and NSArray.

我想知道如何做的:

我要投我 addressDict 正确,使接受NSUserDefaults的我addressDict。虽然,我知道,字典,数组,和Int可铸造成它的NS同行作为独立的实体,但我不知道该怎么做,完全作为数据类型词典&LT;诠释,阵列&LT;字符串&GT;&GT;()。任何帮助吗?例如,我知道这是完全错误的,但像的NSDictionary&LT; NSInteger的,NSArray的&LT;串GT;&GT; 或东西。

I want to cast my addressDict properly so that NSUserDefaults accepts my 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>>(). Any help? For example, I know this is completely wrong but something like NSDictionary<NSInteger, NSArray<String>> or something.

推荐答案

要存储在 NSUserDefaults的字典你需要将它们转换为的NSData 第一。 的NSKeyedArchiver NSKeyedUnarchiver 是完美的人选。下面的示例演示如何存储你的字典中 NSUserDefaults的并重新读取。

To store a dictionary in NSUserDefaults you need to convert them to NSData first. NSKeyedArchiver and NSKeyedUnarchiver are perfect for the job. The following example shows how to store your dictionary in NSUserDefaults and read it back.

    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)
    }

这篇关于存储Int的NSUserDefaults的中误差字典和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆