简体   繁体   English

在 SwiftUI 中的 onDelete 执行中添加 func

[英]Add func in onDelete's perform in SwiftUI

The code is as following:代码如下:

Section(header: Text("Head")) {
    ForEach() { child in
        ...
    }
    .onDelete(perform: self.db.delete)  <-- how to add other func in perform???
}

And I want to add some other func when onDelete occurs like:我想在onDelete发生时添加一些其他功能,例如:

    .onDelete(perform: self.db.delete,
        otherFunc()
    )

func otherFunc() {
}

The self.db.delete after perform is not easy to move to otherFunc() and I want to keep it not changing. perform后的self.db.delete不容易移动到otherFunc() ,我想让它保持不变。

Then how to make it?那么如何制作呢? Thanks for any help.谢谢你的帮助。

You can create another function and pass to it the same parameters from onDelete(perform:) :您可以创建另一个 function 并将来自onDelete(perform:)的相同参数传递给它:

Section(header: Text("Head")) {
    ForEach() { child in
        ...
    }
    .onDelete(perform: delete)
}

And inside call self.db.delete with the same parameters as originally.并在内部调用self.db.delete并使用与最初相同的参数。

func delete(at offsets: IndexSet) {
    self.db.delete(offsets)
    otherFunction()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM