Why the type of "testScores["dave"]" is "Optional<Array<Int>>.Type" instead of "<Array<Int>>.Type"

71 views Asked by At

I'm new to Swift and is trying to learn the concept of subscript. When I tried to find out the returned value of testScores["dave"], I was expecting Array<Int>.Type, however, the IDE instead outputted Optional<Array<Int>>.Type.

Why this is the case? Am I missing something?

var testScores = ["dave": [82, 84, 86], "jen": [23, 14, 5], "ben": []]

testScores["dave"].dynamicType
1

There are 1 answers

0
Kyle Redfearn On BEST ANSWER

Anytime you get a value from a Dictionary it will be optional. It might or might not have an entry for the specified key. That is why you have to unwrap the optional before you can use it.