snapshot.value的类型为
Any。下标是一种特殊的函数,它使用将值括在大括号中的语法。此下标功能由实现
Dictionary。
因此,这里发生的是,作为开发人员的YOU知道这
snapshot.value是一个
Dictionary,但是编译器却没有。它不会让您调用该
subscript函数,因为您试图在type的值上调用它
Any并且
Any未实现
subscript。为此,您必须告诉编译器您
snapshot.value实际上是一个
Dictionary。此外,还
Dictionary可以让您将下标函数与
Dictionary键的类型无关。因此,您需要告诉它您有一个
Dictionary带有
String(AKA
[String:Any])的键。会比这还要进一步,你的情况,你似乎知道,都在你的价值观
Dictionary是
String一样,所以代替铸造每个值,你就下标后
String使用
as!String,如果只是告诉您同时
Dictionary具有两种
String类型的键和值(AKA
[String:String]),那么您将可以通过下标访问这些值,并且编译器将知道这些值
String也是!
guard let snapshotDict = snapshot.value as? [String: String] else { // Do something to handle the error // if your snapshot.value isn't the type you thought it was going to be. }let employerName = snapshotDict["employerName"]let employerImage = snapshotDict["employerImage"]let uid = snapshotDict["fid"]在那里,您拥有了!



