SwiftMatt
SwiftMatt

Reputation: 1029

Cannot subscript a value of type '[String: AnyObject]' with an index of type 'String'

This is not a duplicate of the question

Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String'

or

Cannot subscript a value of type '[String : String]?' with an index of type 'String'

The reason is because my dictionary is not optional, which was the problem with that question. Why is it that I can't subscript this...

enter image description here

Upvotes: 1

Views: 803

Answers (1)

Alain T.
Alain T.

Reputation: 42143

Accessing dictionaries with a subscript always returns an optional. This is because you can't tell the dictionary that your are always going to use a subscript that exists in the keys list. (even if you could, it wouldn't believe you :).

Upvotes: 0

Related Questions