【发布时间】:2015-03-20 01:43:28
【问题描述】:
我有这样声明的字典:
var friendLetterCount:NSDictionary = NSDictionary(dictionary: [String:Int]())
我显然想检索字典中键的信息,但是当我这样做时,它只返回可选整数,我该如何使它们成为整数?
我这样检索:
friendLetterCount["a"]
当我将它打印到控制台时,它会说
optional(1)
我已经尝试以我能想象到的所有方式对其进行转换,但它总是给我一个错误提示
'(NSObject, AnyObject)' is not convertible to 'Int'
任何建议将不胜感激。
【问题讨论】:
-
这几乎是 stackoverflow.com/questions/25979969/… 的副本(仅使用 NSDictionary 而不是 Dictionary)。 – Optional 是 Swift 语言的一个关键概念,你真的应该阅读文档并学习如何处理它们。
标签: swift dictionary int type-conversion nsdictionary