ios - 尝试删除最后一个元素时,带有子项的 SwiftUI (2.0) 列表崩溃

标签 ios swift swiftui ios14

我有以下带有子项的 SwiftUI 列表的代码,改编自另一个 StackOverflow 答案:

SwiftUI 2.0 List with children - how to make the tappable area of the disclosure button cover the whole list item

struct ContentView: View {

    @EnvironmentObject var goodies: GlobalGoodies
    
    func listItem(for item: User) -> some View {
        Text(item.name)
    }
    
    var body: some View {
        NavigationView {
            List {
                ForEach(Array(goodies.users.enumerated()), id: \.1.id) { i, group in
                    DisclosureGroup(isExpanded: $goodies.users[i].isExpanded) {
                        ForEach(group.children ?? []) { item in
                            listItem(for:item)
                        }
                        .opacity(goodies.users[i].opacity)
                    } label: {
                        listItem(for: group)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            withAnimation {
                                goodies.users[i].opacity = goodies.users[i].isExpanded ? 0.0 : 1.0
                                goodies.users[i].isExpanded.toggle()
                            }
                        }
                    }
                }
                .onDelete(perform: delete)
            }
        }
    }

    func delete(at offsets: IndexSet) {
        goodies.users.remove(atOffsets: offsets)
    }
}

GlobalGoodies 类和 User 类的源代码,改编自 HackingWithSwift教程示例。

struct User: Identifiable {
    let id = UUID()
    var name: String
    var children: [User]? = nil
    
    var isExpanded = false
    var opacity = 0.0
}

class GlobalGoodies: ObservableObject {
    @Published var users = [
        User(name: "Paul", children: [
            User(name: "Jenny")
        ]),
        User(name: "Taylor", children: [
            User(name: "Tyler"),
            User(name: "Luna")
        ]),
        User(name: "Adele", children: nil)
    ]
}

我能够正常查看这些元素,并且能够毫无问题地删除两个(父)项目。但是,当我尝试删除列表中最后一个剩余的(父)元素时,应用程序崩溃并显示以下消息:

Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444

我知道其他人在使用 List 时遇到了类似的错误并显示相同的消息,但仅当使用此特定设置来显示可扩展子元素时才会发生此错误。我遵循了上面提到的 HackingWithSwift 教程的代码(这只是一个普通的线性列表),我能够毫无问题地删除其中的所有元素。

我尝试使用新的 List(children:),但由于某种原因,我无法向其中添加 onDelete

任何帮助查明此错误的位置都会很棒,因为我已经尝试放置断点等来查看哪里出了问题。该元素名义上被删除,但崩溃似乎发生在调用 body 期间或之后。

预先感谢您的帮助!

最佳答案

经过一番探索后,我似乎已经能够自己解决这个问题了。此行发生超出范围错误:

DisclosureGroup(isExpanded: $goodies.users[i].isExpanded) {

(我可以通过替换为 .constant(true) 来判断,这阻止了崩溃。)

我能够通过使用一个函数来解决这个问题,该函数在给定索引i的情况下提供自定义绑定(bind),该函数在访问索引之前检查索引是否在范围内:

func binding(for index: Int) -> Binding<Bool> {
        Binding<Bool>(
            get: {
                if index > goodies.users.count - 1{
                    return false
                }
                return goodies.users[index].isExpanded
            },
            set: {
                if index > goodies.users.count - 1 {
                    // do nothing
                } else {
                    goodies.users[index].isExpanded = $0
                }
            }
        )
}

受影响线路的使用情况:

DisclosureGroup(isExpanded: binding(for: i)) {

这解决了崩溃问题。之前有点难以确定,因为我没有意识到在删除列表项后绑定(bind)被访问。希望这对处于相同位置的人有所帮助。

关于ios - 尝试删除最后一个元素时,带有子项的 SwiftUI (2.0) 列表崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312802/

相关文章:

swift - SwiftUI 中的计算 (NSObject) 属性不更新 View

ios - Swift UI 中的动画文本

ios - 无法从 Facebook 帐户工具包获取短信代码

swift - 根据 tvOS 深色模式或浅色模式更改图像?

ios - 动画 View 以在 SwiftUI 中向上滑动和隐藏

Swift:自定义搜索栏布局问题

swift - 我的 scrollViewDidScroll 函数没有收到调用

ios - swift fetchAssetsInAssetCollection : Loading Photos From Range in Camera Roll

ios - 调用 `errno` 后测试 `strtol` 返回 "No such process"

ios - 自动续订的应用内购买订阅如何运作?