ios - Can't seem to save local array in Swift
问题描述
So I'm running into a problem where I can't seem to save the contents of a local array outside of a for loop. This code iterates through notifsTop (which is a dictionary) and saves the values into another array. However, outside of the for loop, the contents of tempnotifsarray are empty, which doesn't make sense to me since I appended them to an array that is outside of the loop block. I have been struggling with this for some time and can't figure out what is going on. Any help would be appreciated, thanks!
func createArray() -> [notificationLabel] {
let newUserInfo = Auth.auth().currentUser
let uid = newUserInfo?.uid
self.ref = Database.database().reference()
let practionerRef = self.ref.child("users").child(uid!)
var tempnotifsArray: [notificationLabel] = []
practionerRef.observeSingleEvent(of: .value, with: {(snapshot) in
let value = snapshot.value as? NSDictionary
if let notifsTop = value?["Notifications"] as? NSDictionary { //top of the notifications hierarchy
for (_, myValue) in notifsTop {
// Iterate in here
//self.notifications.append(myValue)
let notification = notificationLabel(label: myValue as! String)
tempnotifsArray.append(notification)
//if I print here the array is full with the values I want
}
}
})
print(tempnotifsArray) //comes out as []
return tempnotifsArray
}
解决方案
推荐阅读
- python - TicTacToe 再次播放时如何重置棋盘?
- c - 向用户询问数据并将它们存储在 C 中的结构中
- flutter - Flutter Youtube 视频上传
- javascript - 科尔多瓦插件 ble-central 的问题
- java - Java Eclipse LocalDate 无法解析为类型?
- typo3 - v:page 不想显示自定义页面 TYPO3 9.5.19 VHS: 6.0.3
- c++ - 字符串比较
- python - 按周和产品分组,并对 Pandas 中的 ID 求和
- scala - 高效使用 Apache Spark 将数据推送到 elasticsearch
- postgresql - 无法通过 PSQL 或 php 连接到 postgres,只有 pgAdmin4