0

I have a severe problem with the SwiftUI List view in Xcode 12 (beta) (MacOS App).

When a List item, which is selected, is removed, the List crashes every time.

"[General] Row 2 out of row range [0-1] for rowViewAtRow:createIfNeeded:"

Looks like a bug in SwiftUI to me. What can I do to prevent the crash? I've tried several things already, but with no success.

Example code:

//
// 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
                }
            })
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
ikemuc
  • 43
  • 8
  • No crash with Xcode 11.7 / macOS 10.15.6. Just in case. – Asperi Sep 12 '20 at 13:31
  • To reproduce, you have to select the last item in the list and hit the delete button. In my environment, it crashed 100% sure. I'm using Xcode 12 beta6 on a Mac Book Air running the latest MacOS Big Sur Beta (build 20A5364e). – ikemuc Sep 12 '20 at 14:49
  • Did another check: I created a new, clean MacOS App project in Xcode, put the example code in ContentView.swift and exchanged the ContentView() in the App by MyView(). Then choose build profile MacOS and start. After that select the last entry and hit the Delete button and Boom, it crashes... – ikemuc Sep 12 '20 at 14:58
  • Did 2 Checks now: 1. MacOS 11 beta (20A5364e) + Xcode 11.7 => Crash 2. MacOS 10.15.6 + Xcode 11.7 => no Crash The bug is clearly in MacOS 11 beta... Thanks for helping, even when there's no solution yet, but it's clear now, that it's not my fault... :-) – ikemuc Sep 12 '20 at 22:55
  • Filed Apple bug report (FB8680646)... – ikemuc Sep 12 '20 at 23:27

1 Answers1

0

Re-tested after installing MacOS 11.0 Beta 7 (20A5374g). The example code doesn’t crash any more, the bug seems to be fixed.

Thanks to all for testing and so giving me the hint, that it's a MacOS beta bug. :-)

ikemuc
  • 43
  • 8