Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios .ondelete SwiftUI列表及其部分_Ios_Swift_Swiftui - Fatal编程技术网

Ios .ondelete SwiftUI列表及其部分

Ios .ondelete SwiftUI列表及其部分,ios,swift,swiftui,Ios,Swift,Swiftui,我无法释放用于删除和移动SwiftUI列表中包含节的行的方法 我有以下类别的模型: struct Category: Identifiable { var id = UUID() var title: String var number: Int var items: [ChecklistItem] func deleteListItem(whichElement: IndexSet) { items.remove(atOffsets: w

我无法释放用于删除和移动SwiftUI列表中包含节的行的方法

我有以下类别的模型:

struct Category: Identifiable {

    var id = UUID()
    var title: String
    var number: Int
    var items: [ChecklistItem]

    func deleteListItem(whichElement: IndexSet) {
      items.remove(atOffsets: whichElement)
    }

    func moveListItem(whichElement: IndexSet, destination: Int) {
      items.move(fromOffsets: whichElement, toOffset: destination)
    }


}
核对清单项目:

struct ChecklistItem: Identifiable {
  
  let id = UUID()
  var name: String
  var isChecked = false
    
}
和检查表:

class Checklist: ObservableObject {

  @Published var items = [Category]()
}
这是我对列表的看法:

struct ChecklistView: View {

  @EnvironmentObject var checklist: Checklist
  @State var newChecklistItemViewIsVisible = false

  var body: some View {
    NavigationView {
      List {
        ForEach(checklist.items) { category in
            Section(header: Text(category.title)) {
                ForEach(category.items) { item in
                    HStack {
                      Text(item.name)
                      Spacer()
                      Text(item.isChecked ? "✅" : "You need to use 
mutating
modifier for your functions, and updated code

struct Category: Identifiable {

    // ... other code

    mutating func deleteListItem(_ whichElement: IndexSet) {
      items.remove(atOffsets: whichElement)
    }

    mutating func moveListItem(_ whichElement: IndexSet, _ destination: Int) {
      items.move(fromOffsets: whichElement, toOffset: destination)
    }
}
struct ChecklistView:View{
@环境对象变量检查表:检查表
@状态变量newChecklistItemViewIsVisible=false
var body:一些观点{
导航视图{
名单{
ForEach(检查表项目){类别在
章节(标题:文本(类别.标题)){
ForEach(category.items){item in
HStack{
文本(项目名称)
垫片()

文本(item.isChecked?)✅" : " 您需要为您的函数使用
mutating
修饰符,并更新代码

.onDelete { indexSet in 
   checklist.items[category.number].deleteListItem(indexSet) 
}
.onMove { indexSet, dest in 
   checklist.items[category.number].moveListItem(indexSet, dest) 
}
和用法一样


使用Xcode 12进行测试。我没有更改其他代码。这可能是在您修改代码时出现的。我也使用Xcode 12,只是更改了.onDelete(执行:checklist.items[category.number].deleteListItem)和.onMove(执行:checklist.items[category.number].moveListItem)但无论如何都会出现这个错误。如果我使用我的方法,我会出现错误部分应用“mutating”方法是不允许的。提供的代码快照的两个部分都是有意义的-你必须对它们都进行调整。第一个允许类别变化,第二个修复部分…错误。