单击按钮时会显示警报。我从列表中选择(如果有多少数据可用。)如何保存我从列表索引中选择的数据?
您可以在here上看到UI
我的帐户服务
class accountServices {
static let databaseReference = Database.database().reference(withPath: "accounts").child((Auth.auth().currentUser?.uid)!)
static var account = account()
static func saveChanges() {
databaseReference.setvalue(try! FirebaseEncoder().encode(accountServices.account))
}
static func getaccount() {
databaseReference.observeSingleEvent(of: .value,andPreviousSiblingKeyWith: { (snapshot,_) in
account = try! FirebaseDecoder().decode(account.self,from: snapshot.value!)
})
}
}
变量
var product: ProductViewModel?
addButton点击
@IBaction func addToCartButtonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: "Bu ürünü hangi sepetinize eklemek istersiniz ?",message: "",preferredStyle: .actionSheet)
var indexer = 0
for cart in accountServices.account.cart! {
if cart.product == nil{
accountServices.account.cart![indexer].product = [Product]()
}
let action = UIAlertaction(title: cart.name,style: .default,handler: { (sender) in
if let index = alert.actions.firstIndex(where: { $0 === sender }) {
accountServices.account.cart?[index].product?.append(self.product) `//Error: Cannot convert value of type 'ProductViewModel?' to expected argument type 'Product'`
accountServices.saveChanges()//TODO...
}
let addAlert = UIAlertController(title: "Sepetinize Eklendi.",message: "Ürününüz sepetinize eklendi.",preferredStyle: .alert)
let okButton = UIAlertaction(title: "Tamam",handler: nil)
addAlert.addaction(okButton)
self.present(addAlert,animated: true,completion: nil)
})
alert.addaction(action)
indexer += 1
}
let cancelaction = UIAlertaction(title: "Vazgeç",style: .cancel,handler: nil)
alert.addaction(cancelaction)
present(alert,completion: nil)
}
}