我有这个结构:
struct CartItem {
var name: String = ""
var count = Int()
}
var itemsInCart : [CartItem] = []
我用一个按钮附加到它:
@IBAction func addButton(_ sender: UIButton){
if let index = itemsInCart.index(where: { $0.name == name }) {
itemsInCart[index].count += 1
} else {
itemsInCart.append(CartItem(name: name, count: 1))
}
}
现在,我要将其上传到Firebase。当我这样做时:
@IBAction func orderButton(_ sender: UIButton) {
for items in itemsInCart {
//Write to firebase
var dataArray = "\(items.count) \(items.name)"
let data = ["CreatedOn" : timeStamp, "Order" : dataArray] as [String : Any]
self.ref?.child(dateAndTime).updateChildValues(data, withCompletionBlock: { (error, ref) in
if error != nil{
print(error!)
return
}
})
let userData = ["CreatedOn" : timeStamp, "Order" : data] as [String : Any]
break
}
}
此问题是它仅将最后一个附加值写入Firebase。我如何才能包含整个Struct?
如果用户先订购:2个香蕉,然后订购:3个苹果,firebase应显示为:
2根香蕉
3个苹果
现在,它只读取3个苹果。