简体   繁体   English

如果NSDictionary具有键的整数,如何将NSDictionary强制转换为swift字典

[英]How do I cast an NSDictionary to a swift dictionary if the NSDictionary has integers for keys

I have a dictionary stored in a Plist that has an imbedded dictionary in it. 我有一个存储在Plist中的字典,里面有一个嵌入字典。 If I use strings as the keys I can use the code below to cast it to a swift dictionary: 如果我使用字符串作为键,我可以使用下面的代码将其转换为快速字典:

let namesDict = NSDictionary(contentsOfFile: path!)
var names = namesDict as [String: [String : String]]

However if I try to use integers as the keys in the root dictionary I can't get it to work. 但是,如果我尝试使用整数作为根字典中的键,我就无法使其工作。 The code below doesn't work and returns this error: "value type is not bridged to Objective-C" 下面的代码不起作用并返回此错误:“值类型未桥接到Objective-C”

let namesDict = NSDictionary(contentsOfFile: path!)
var names = namesDict as [Int: [String : String]]

I have tried intValue, but that doesn't work either. 我尝试过intValue,但这也不起作用。 What am I missing? 我错过了什么?

From Apple's NSDictionary reference: 来自Apple的NSDictionary参考:

In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see Key-Value Coding Fundamentals). 通常,密钥可以是任何对象(只要它符合NSCopying协议 - 见下文),但请注意,使用键值编码时,密钥必须是字符串(请参阅键值编码基础)。

Plist are Key-Value coding compliant Plist符合键值编码

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

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