I have the following code.
func setUserToken(_ user: NSDictionary) {
self.userId = user["user"]["id"] as! String;
I get the error "Value of type Any has no subscripts". What am I doing wrong?
I have the following code.
func setUserToken(_ user: NSDictionary) {
self.userId = user["user"]["id"] as! String;
I get the error "Value of type Any has no subscripts". What am I doing wrong?
2
Answers
The type of any lookup from a
NSDictionary
isAny
. You know (or think you know) it is another dictionary, but Swift does not. You must establish the type before you can do the second lookup.You can do it in a single line using optional chaining and the nil coalescing operator:
You need to specify your json before parsing it. Try This: