将Int64存储在UserDefaults中 [英] Storing Int64 in UserDefaults

查看:170
本文介绍了将Int64存储在UserDefaults中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了我的字典:

var teamsData = Dictionary<String,Dictionary<String,Int64>>()

然后,我试图将它存储在userdefaults中:

Then, I am trying to store it in userdefaults:

NSUserDefaults.standardUserDefaults().setObject(teamsData, forKey: "teamsData")

但它会抛出错误:

Type Dictionary<String,Dictionary<String,Int64>> does not conform to protocol 'Any Object'


推荐答案

默认对象只能是
NSData NSString 实例的组合) c>, NSNumber NSDate NSArray code> NSDictionary

A user default object can only be an instance (or a combination of instances) of NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.

一些Swift类型自动桥接到基础类型,
eg Int UInt Float 双重 Bool 桥接
NSNumber 。所以这可以保存在用户默认值中:

Some Swift types are automatically bridged to Foundation types, e.g. Int, UInt, Float, Double and Bool are bridged to NSNumber. So this could be saved in the user defaults:

var teamsData = Dictionary<String,Dictionary<String,Int>>()

在64位架构上, Int 是64位整数,但
32位体系结构, Int 是一个32位整数。

On 64-bit architectures, Int is a 64-bit integer, but on 32-bit architectures, Int is a 32-bit integer.

固定大小的整数类型,如 Int64 不是
自动桥接到的NSNumber 。在 Swift - 将Int64转换为AnyObject for NSMutableArray
因此,要在用户默认值中存储64位整数,您有
可以明确使用 NSNumber

The fixed-sized integer types such as Int64 are not automatically bridged to NSNumber. This was also observed in Swift - Cast Int64 to AnyObject for NSMutableArray. Therefore, to store 64-bit integers in the user defaults you have to use NSNumber explicitly:

var teamsData = Dictionary<String,Dictionary<String,NSNumber>>()

// Example how to add a 64-bit value:
let value : UInt64 = 123
teamsData["foo"] = ["bar" : NSNumber(unsignedLongLong: value)]

这篇关于将Int64存储在UserDefaults中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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