swift:嵌套字典有问题
swift: issue with nested dictionary
这是我的代码,我收到运行时错误:
var studentsDictionary: [Int: [String:Int]] = [Int: [String:Int]]()
studentsDictionary[studentsDictionary.count]!["Bob"] = 0
是的,这个结构会有问题。您正在尝试为 studentsDictionary.count
处的字典设置 Bob
键(该键尚不存在,因此尝试使用 !
强制展开会导致问题)。
你可以用
完成你想要的
var studentsDictionary = [Int: [String:Int]]()
studentsDictionary[studentsDictionary.count] = ["Bob": 0]
这是我的代码,我收到运行时错误:
var studentsDictionary: [Int: [String:Int]] = [Int: [String:Int]]()
studentsDictionary[studentsDictionary.count]!["Bob"] = 0
是的,这个结构会有问题。您正在尝试为 studentsDictionary.count
处的字典设置 Bob
键(该键尚不存在,因此尝试使用 !
强制展开会导致问题)。
你可以用
完成你想要的var studentsDictionary = [Int: [String:Int]]()
studentsDictionary[studentsDictionary.count] = ["Bob": 0]