【发布时间】:2020-06-05 05:56:36
【问题描述】:
我有一个 userImages 集合,我正在尝试从中读取数据并将其附加到我的 imgUrls 数组中。
这是我从数据库中读取数据并尝试将其附加到我的数组的代码。不幸的是,我不断收到错误,因为数组显然是空的。
override func viewDidLoad() {
var ref: DatabaseReference!
let userID = Auth.auth().currentUser?.uid //holds the current user uid
ref = Database.database().reference()
var imgUrls = [String]() //array to hold the image urls from the userImages collection
ref.child("userImages").child(userID!).observeSingleEvent(of: .value) { (snapshot) in //read from userImages collection only from the subcollection where the
guard let dict = snapshot.value as? [String: Any] else { return } //document Id equals the current user uid. Create a dictionary from the
//snapshot values
let values = dict.values //holds the values from the dictionary
for value in values { //for loop to go through each value from the dictionary
imgUrls.append((value as? String)!) //and append to the imgUrls array
}
}
testLabel.text = imgUrls[0] //I used this to test, but i get an error saying the array is empty
}
我之前发过一个问题,但是太复杂了我决定删除它并重新发布它更简单。
非常感谢任何帮助!
【问题讨论】:
标签: swift firebase firebase-realtime-database firebase-authentication