swift - 快速从多维数组中获取值

标签 swift dictionary

我在 Swift 中有以下数组:

var words = [
        "English" : ["Hello", "Bye"],
        "Spanish" : ["Hola", "Adios"]
    ]

我怎样才能得到索引的值,下面的东西不起作用

print(words["English"][0])

它抛出错误:可选类型数组的值?没有解开,你的意思是使用!或者 ?但这就是它:

print(words["English"]?[0])

还是不行,请帮忙。

最佳答案

你需要研究如何unwrap optionals .例如,您尝试做的事情可以通过以下两种方式之一完成:

强制展开:

print(words["English"]![0])

安全解包:

if let hello = words["English"]?[0]{
    print(hello)
}

关于swift - 快速从多维数组中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45292349/

相关文章:

swift - 表节标题 : multi-line/word wrapping

swift - 在谓词中计算属性,在 RealmSearchViewController 中作为 sortProperty?

c# - C# 中带有列表的默认字典

for-loop - 在循环中将条目添加到 map

swift - 有没有办法识别数组中的 int 是一位数还是两位数?

ios - 为具有 CenterX 约束的 View 设置自定义高度

swift - 具有私有(private)构造函数的公共(public) OptionSet

c++ - 根据数据对 std::map 进行排序(map->vector->sort)

java - 为什么 Map.of 不允许空键和值?

ios - 在 MKMapView 上更新和组织覆盖