【发布时间】:2017-07-12 03:21:09
【问题描述】:
我在类文件中声明了一个字典,如下所示:
var myDict: [Int: String] = [:]
我还做了一个函数来更改字典中的值:
func firebaseToDictionary() {
let uid = FIRAuth.auth()?.currentUser?.uid
let infoRef = FIRDatabase.database().reference().child("users").child(uid!).child("info")
infoRef.observe(FIRDataEventType.value, with: { (snapshot) in
let postDict = snapshot.value as? [String: String]
for item in postDict! {
let theString = item.value["FirebaseString"]
let theNumber = item.value["FirebaseInt"]
self.myDict[theNumber] = theString
//First Print
print(myDict)
}
})
//Second Print
print(myDict)
}
在//First Print 处,字典包含新值并且看起来不错,但//Second Print 显示字典创建时的样子;作为[:]。 //Second Print 实际上在 //First Print 之前出现在终端中
为什么我的字典在函数内部更改后仍显示为 [:]?
【问题讨论】:
标签: swift dictionary firebase firebase-realtime-database firebase-authentication