SwiftUI列表在删除选定项目时崩溃



我在Xcode 12(测试版((MacOS应用程序(中的SwiftUI列表视图出现严重问题。

删除选定的"列表"项时,"列表"每次都会崩溃。

"[常规]第2行超出rowViewAtRow:createIfNeeded:"的行范围[0-1];

对我来说,SwiftUI中似乎有一个错误。我该怎么做才能防止崩溃?我已经尝试了好几种方法,但都没有成功。

示例代码:

//
// Example to reproduce bug
// * Select no item or other than last item and press button: selection is reset, last item is removed, no crash
// * Select last list item and press button "Delete last item" => Crash
//
import SwiftUI
class MyContent: ObservableObject {
@Published var items: [String] = []
@Published var selection: Set<String> = Set()

init() {
for i in 1...5 {
self.items.append(String(i))
}
}
}
struct MyView: View {
@ObservedObject var content: MyContent = MyContent()

var body: some View {
VStack {
List(content.items, id: .self, selection: $content.selection) {
item in
Text("(item)")
}

Button("Delete last item", action: {
if content.items.count > 0 {
content.selection = Set()  // reset selection
var newItems = Array(content.items)
newItems.removeLast()
content.items = newItems
}
})
}
}
}

安装MacOS 11.0 Beta 7(20A5374g(后重新测试。示例代码不再崩溃,错误似乎已经修复。

感谢所有的测试,所以给了我一个提示,这是一个MacOS测试版错误。:-(

最新更新