let employerName = snapshot.value! ["employerName"] as! String
let employerImage = snapshot.value! ["employerImage"] as! String
let uid = snapshot.value! ["uid"] as! String
I reviewed previous posts but cannot seem to find a way to solve this problem. All three lines of code give the "type 'Any' has no subscript members" error. Fairly new to this so any help is appreciated.
snapshot.value
has a type ofAny
. A subscript is a special kind of function that uses the syntax of enclosing a value in braces. This subscript function is implemented byDictionary
.So what is happening here is that YOU as the developer know that
snapshot.value
is aDictionary
, but the compiler doesn't. It won't let you call thesubscript
function because you are trying to call it on a value of typeAny
andAny
does not implementsubscript
. In order to do this, you have to tell the compiler that yoursnapshot.value
is actually aDictionary
. Further moreDictionary
lets you use the subscript function with values of whatever type theDictionary
's keys are. So you need to tell it you have aDictionary
with keys asString
(AKA[String: Any]
). Going even further than that, in your case, you seem to know that all of the values in yourDictionary
areString
as well, so instead of casting each value after you subscript it toString
usingas! String
, if you just tell it that yourDictionary
has keys and values that are bothString
types (AKA[String: String]
), then you will be able to subscript to access the values and the compiler will know the values areString
also!And there you have it!
Since you want to treat
snapshot.value
as an unwrapped dictionary, try casting to one and, if it succeeds, use that dictionary.Consider something like: