【问题标题】:Add multiple actions to a Core - Data relationship at one time一次将多个操作添加到核心 - 数据关系
【发布时间】:2018-08-07 17:20:16
【问题描述】:

在我的应用程序中应该能够向多个成员添加操作。我已经能够选择多个成员并将它们推送到下一个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”

我应该怎么做?

【问题讨论】:

    标签: ios swift xcode core-data


    【解决方案1】:

    对于数组,您必须遍历每个项目

    for member in members {
        member.addToRawTransactions(transaction)  
    }
    

    您是否知道您正在创建两次相同的事务,也许您应该删除let transaction = Transaction(... 调用之一?

    【讨论】:

    • 完美。我第一次挣扎是因为我直接在if let 行下添加了您的解决方案,但我也必须使用if let 进行循环。非常感谢您的帮助。
    • 而且我不知道我正在执行两次 let 交易。我真的很困惑,但由于这是我的第一个应用程序,我不确定是否可以删除它。也谢谢你。
    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2014-08-13
    相关资源
    最近更新 更多