I have the following swift dictionary
我有以下快速字典
var List = [
2543 : [ "book", "pen" ],
2876 : [ "school", "house"]
]
How can i access the array values ?
我如何访问数组值?
println(List[2543][0])
The above code gives error "could not find member subscript"
上面的代码给出了“无法找到成员下标”的错误
and it should print "book"
它应该打印“书”
3 个解决方案
#1
7
Note that subscript
returns an optional. We have to force unwrapping:
请注意,下标返回一个可选项。我们必须强行打开:
println(list[2543]![0])
Or use optional chaining
或使用可选链接
println(list[2543]?[0])
#2
3
println(list[2543]![0])
Remember, dictionary subscript returns an Optional, not an array or whatever is inside the dictionary value.
请记住,字典下标返回一个Optional,而不是数组或字典值中的任何内容。
#3
0
Just try with following code:
试试以下代码:
var dic = List[0];
println("values \(dic)")
OR
要么
println(list[2543]![0])
#1
7
Note that subscript
returns an optional. We have to force unwrapping:
请注意,下标返回一个可选项。我们必须强行打开:
println(list[2543]![0])
Or use optional chaining
或使用可选链接
println(list[2543]?[0])
#2
3
println(list[2543]![0])
Remember, dictionary subscript returns an Optional, not an array or whatever is inside the dictionary value.
请记住,字典下标返回一个Optional,而不是数组或字典值中的任何内容。
#3
0
Just try with following code:
试试以下代码:
var dic = List[0];
println("values \(dic)")
OR
要么
println(list[2543]![0])