我正在尝试从 Firebase Firestore 中提取所有对象(参见下图)。如何将输出结果添加到字典中?
func getLinks() {
let user = Auth.auth().currentUser
let userID = user?.uid
let db = Firestore.firestore()
print(userID!)
let docRef = db.collection("files").document(userID!)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
}
}
最佳答案
从 Firebase 获取数据然后使用它
步骤:-1获取数据
var dictData = [String:Any]()
let ref = Database.database().reference()
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot.value!)
self.dictData = snapshot.value as! [String : Any]
print(self.dictData)
})
步骤:- 2从快照中提取数据
var arrFetchedData:NSMutableArray = NSMutableArray()
for data in dictData{
print(data.key) //Key will be printed here
var tempDict:NSMutableDictionary = NSMutableDictionary()
let innerData = data.value as! [String:Any]
let expires = innerData["expires"]
tempDict.setValue(expires, forKey: "expires")
let name = innerData["name"]
tempDict.setValue(name, forKey: "name")
let url = innerData["url"]
tempDict.setValue(url, forKey: "url")
arrFetchedData.add(tempDict)
}
print(arrFetchedData) //All the Fetched data will be here
希望这有帮助!
关于ios - 如何从 Firebase Cloud Firestore 提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49720335/