var dist: Dictionary<String, NSObject>!
var bigDist : Dictionary<String, Dictionary<String, NSObject>>!
self.bigDist["1" as String] = self.dist
当我尝试这样做时,我收到一个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
self.dist
的数据集:
Optional(["hits": 0, "name": India lags behind China in consumer spends: Stefano Ricci’s Jackie Manglani, "web": Livemint, "id": -1655, "like": 0, "iurl": http://www.livemint.com/rf/Image-222x148/LiveMint/Period2/2017/02/21/Photos/Processed/jackiemanglani2-kXhC--621x414@LiveMint.JPG, "notify": 0, "date": 201655200217, "active": 1, "type": Mark, "dateAdded": 20/02/2017, "url": http://www.livemint.com/Consumer/3NT8Vbw7BcGeoPJNMoyK5H/India-lags-behind-China-in-consumer-spends-Stefano-Riccis.html])
答案 0 :(得分:0)
您的变量bigDist
是一个隐式解包的可选项。 (你用your_type!
声明它。
这意味着:
?
或!
打开它。3号正在发生。
您需要创建一个空字典。尝试删除声明中的!
,并在声明时创建一个空字典:
var bigDist : Dictionary<String, Dictionary<String, NSObject>> = [:]
答案 1 :(得分:0)
您没有初始化bigDist
:
var dist: Dictionary<String, NSObject>? = nil
var bigDist = Dictionary<String, Dictionary<String, NSObject>>()
self.bigDist["1"] = self.dist
无论如何,代码可能更加迅速。使用[String: NSObject]
代替Dictionary<String, NSObject>
(完全相同)会使代码更易于阅读,并且可以像您一样防止出现这样的错误。
var dist: [String: NSObject]? = nil
var bigDist = [String: [String: NSObject]]()
self.bigDist["1"] = self.dist