ios - 一次向核心 - 数据关系添加多个操作

标签 ios swift xcode core-data

在我的应用程序中,应该能够向多个成员添加操作。我已经能够选择多个成员并将它们推送到下一个 ViewController,用户可以在其中添加操作。我已经为一个成员(member)提供了这个功能。

有一个成员的代码:

 @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) 但随后出现错误:

Value of type '[Member]' has no member 'addToRawTransactions'

我应该怎么做?

最佳答案

对于数组你必须遍历每一项

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

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

关于ios - 一次向核心 - 数据关系添加多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732232/

相关文章:

ios - CAShapeLayer路径颜色变化

ios - UITableViewCell 打嗝中的自动播放视频

xcode - 场景编辑器不显示网格

ios - 在 UIView 中切一个洞

ios - 为什么使用 kCGImagePropertyGIFDelayTime 不会减慢 GIF 速度

ios - 创建 tableview 所有数据的 pdf

Swift 使用协议(protocol)扩展默认值

ios - 在 Firebase 中更改存储元数据中的值

xcode - 升级 iOS SDK

objective-c - 我如何检查 ALAsset 是一个图像。我如何从 ALAssetGroup 中分离图像(.png)文件