在我的应用中,应该可以向多个成员添加操作。我已经可以选择多个成员并将其推送到下一个ViewController
,用户可以在其中添加操作。我已经有一位成员使用此功能。
有一个成员的代码:
@IBAction func addTransactionButton(_ sender: UIBarButtonItem) {
let transaction = Transaction(money: Double(moneyTextField.text!) ?? 0.0, date: datePicker.date , reason: reasonTextField.text ?? "")
do{
try transaction?.managedObjectContext?.save()
} catch{
print("Could not save transaction")
}
let reason = reasonTextField?.text
let moneyText = moneyTextField?.text ?? ""
let money = Double(moneyText) ?? 0.00
let date = datePicker?.date
if let transaction = Transaction(money: money, date: date, reason: reason) {
member?.addToRawTransactions(transaction)
do{
try transaction.managedObjectContext?.save()
self.navigationController?.popViewController(animated: true)
} catch{
print("Transaction could not be created")
}
}
}
我尝试将member?.addToRawTransactions(transaction)
替换为members?.addToRawTransactions(transaction)
,但出现错误:
“ [Member]”类型的值没有成员“ addToRawTransactions”
我该怎么办?
答案 0 :(得分:2)
对于数组,您必须遍历每个项目
for member in members {
member.addToRawTransactions(transaction)
}
您是否知道要创建两次相同的交易,也许您应该删除let transaction = Transaction(...
调用之一?