作者:方彦 | 来源:互联网 | 2023-06-17 09:29
需要有关Firebase dict错误的帮助。已经尝试了与该问题相关的一些解决方案,但没有奏效。
我想使用AlertView获取和更新表中的数据,但是我在 addtaskpop()中将字典作为调用中的额外参数'data'时遇到错误在下面标记的提交动作中起作用
Secondviewcontroller.swift
func addtaskpop() {
let db = Firestore.firestore()
let alertCOntroller= UIAlertController(title: "Task",message: "Enter your Task and Amount",preferredStyle: .alert)
let submit = UIAlertaction(title: "Enter",style: .default)
{
(_) in
let task1 = alertController.textFields?[0].text
let amount1 = alertController.textFields?[1].text
let income1 = alertController.textFields?[2].text
self.dev = income1!
print("dev is \(self.dev)")
let amountcolor = Int(amount1!)
self.income.isHidden = false
// let incomeint = Int(income1!)
// var incometotal = incomeint
// incometotal = (incomeint + incomeint)
self.income.text = ("Income is Rs. \(self.dev)")
self.amount.append(amount1!)
let text = amount1
let textInt = Int(text!)
self.numbers.append(textInt!)
let total = self.numbers.reduce(0,+)
let amountstring = String(total)
self.totalamount.text = ("Rs.\(amountstring)")
let date = Date()
let format = DateFormatter()
format.dateFormat = "dd-MM-yyyy HH:mm"
let formattedDate = format.string(from: date)
print(formattedDate)
self.poptaskarray.append(task1!)
self.date.append(formattedDate)
self.mytableView.reloadData()
var docRef: DocumentReference? = nil
docRef = db.collection("Tasks").addDocument(data: [ **Extra argument 'data' in call**
"expensename": task1.text ?? "empty task",// "expensename": task1.text ?? "empty task","minus": amount1.text ?? "empty task","date": "\(formattedDate!)","amount": self.totalamount.text ?? "empty task","income": income.text ?? "empty task",]) { err in
if let err = err {
print("Error adding document: \(error.localizedDescription)")
} else {
print("Document added with ID: \(docRef!.documentID)")
}
}
}
let cancelaction = UIAlertaction(title: "Cancel",style: .destructive) { (_) in
}
alertController.addTextField { (textField) in
textField.placeholder = "Enter your task"
}
alertController.addTextField { (textField) in
textField.placeholder = "Enter your amount"
textField.keyboardType = .numberPad
// textField.text = "Rs. "
}
alertController.addTextField { (textField) in
textField.placeholder = "Enter your income"
textField.keyboardType = .numberPad
}
alertController.addaction(cancelaction)
alertController.addaction(submit)
//finally presenting the dialog box
self.present(alertController,animated: true,completion: nil)
}
}
我已经创建了 Task.swift
import Foundation
struct Task{
var expensename:String
var done: Bool
var date: String
var id: String
var amount: String
var income: String
var minus: String
var dictionary: [String: Any] {
return [
"expensename": expensename,"done": done,"date": date,"amount": amount,"income": income,"minus": minus
]
}
}
extension Task{
init?(dictionary: [String : Any],id: String) {
guard
let expensename = dictionary["expensename"] as? String,let dOne= dictionary["done"] as? Bool,let date = dictionary["date"] as? String,let amount = dictionary["amount"] as? String,let income = dictionary["income"] as? String,let minus = dictionary["minus"] as? String
else {
return nil
}
self.init(expensename: expensename,done: done,date: date,id: id,amount: amount,income: income,minus: minus)
}
}
感谢您的帮助!
感谢和问候