gpt4 book ai didi

ios - 如何在 Firebase 中检索自动生成的 ID/ key ?

转载 作者:行者123 更新时间:2023-11-28 15:40:51 25 4
gpt4 key购买 nike

我正在使用以下方法获取 firebase 数据库中的所有产品,但我需要获取自动生成的 key 。我应该怎么做才能做到这一点?

func fetchProducts(){
self.ref.child("products").observe(.childAdded, with: { (snapshot) in
//print(snapshot)
let proObject = snapshot.value as! NSDictionary

let description = proObject["description"] as! String
let categories = proObject["category"] as! Array<String>
let images = proObject["image"] as! Array<String>
let name = proObject["name"] as! String
let price = proObject["price"] as! Int
let qty = proObject["qty"] as! Int

let pro = Product(name: name, qty: qty,
images: images, price: price, category: categories, description: description)

self.items.append(pro)
})
}

最佳答案

自动生成的 key 在 snapshot.key 属性中可用。

Firebase documentation对于此属性:

The key of the location that generated this FIRDataSnapshot.

关于ios - 如何在 Firebase 中检索自动生成的 ID/ key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com