删除所选项目时 SwiftUI 列表崩溃
SwiftUI List crashes when removing a selected item
我在 Xcode 12(测试版)(MacOS 应用程序)中遇到 SwiftUI 列表视图的严重问题。
删除选中的列表项时,列表每次都会崩溃。
[常规] rowViewAtRow:createIfNeeded 的第 2 行超出行范围 [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
}
})
}
}
}
Re-tested 安装 MacOS 11.0 Beta 7 (20A5374g) 后。
示例代码没有再崩溃了,bug似乎已经修复了。
感谢大家的测试并给我提示,这是一个 MacOS 测试版错误。 :-)
我在 Xcode 12(测试版)(MacOS 应用程序)中遇到 SwiftUI 列表视图的严重问题。
删除选中的列表项时,列表每次都会崩溃。
[常规] rowViewAtRow:createIfNeeded 的第 2 行超出行范围 [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
}
})
}
}
}
Re-tested 安装 MacOS 11.0 Beta 7 (20A5374g) 后。 示例代码没有再崩溃了,bug似乎已经修复了。
感谢大家的测试并给我提示,这是一个 MacOS 测试版错误。 :-)