SwiftUI Hierarchical List on macOS represents incorrect results.

A item is replaced by the last uncle.


Conditions are:
  1. Target to macOS

  2. The item "X" is placed next to the folded sibling with children.

  3. The parent of the "X" is not the last sibling.

  4. The last uncle "Y" has no children.

then, "X" will be replaced by "Y", and "Y" appears twice.

It's amazing that the very fundamental operations are broken.
As this issue only occurs on macOS, it is not reproduced on playground preview or iOS.

Code Block swift
import SwiftUI
struct TreeViewTest: View {
    var items: [Item]
    var body: some View {
        List(
            items,
            children: \.children
        ) { item in
            Text(item.id)
        }
    }
    struct Item: Hashable, Identifiable {
        var id: String
        var children: [Self]?
    }
}
struct TreeViewTest_Previews: PreviewProvider {
    static var previews: some View {
        TreeViewTest(items: [
            .init(id: "0", children: [
                .init(id: "00"),
                .init(id: "01", children: [
                    .init(id: "010"),
                    .init(id: "011"),
                    .init(id: "012")
                ]),
                .init(id: "02"),
                .init(id: "03")
            ]),
            .init(id: "1")
        ])
    }
}


It represents:
Code Block plain
-0
--00
-+01
--1 /* <- should be 02 */
--03
-1

  • Is there anything wrong with my code?

  • If it is a bug, is there a temporary hacky workaround?

Same problem in 2022.9.12

SwiftUI Hierarchical List on macOS represents incorrect results.
 
 
Q