仅在macOS 15.1下带子项的List/Table组件折叠报错问题
macOS 15.1下SwiftUI折叠List/Table层级条目触发索引越界错误
问题现象
- 可正常显示所有无父项条目及带子项的条目
- 尝试折叠任何带子项的条目时,触发
Row index -1 out of row range错误
环境排查结论
- 仅在macOS 15.1编译运行时出现,iOS 18.1下无此问题
- 单独使用
OutlineGroup不嵌套List时无错误 - 使用
Table组件时同样触发该错误
复现代码(Xcode List组件QuickHelp示例)
struct ContentView: View { struct FileItem: Hashable, Identifiable, CustomStringConvertible { var id: Self { self } var name: String var children: [FileItem]? = nil var description: String { switch children { case nil: return "📄 \(name)" case .some(let children): return children.isEmpty ? "📂 \(name)" : "📁 \(name)" } } } let fileHierarchyData: [FileItem] = [ FileItem(name: "users", children: [FileItem(name: "user1234", children: [FileItem(name: "Photos", children: [FileItem(name: "photo001.jpg"), FileItem(name: "photo002.jpg")]), FileItem(name: "Movies", children: [FileItem(name: "movie001.mp4")]), FileItem(name: "Documents", children: []) ]), FileItem(name: "newuser", children: [FileItem(name: "Documents", children: []) ]) ]), FileItem(name: "private", children: nil) ] var body: some View { List(fileHierarchyData, children: \.children) { item in Text(item.description) } } }
报错信息
Row index -1 out of row range (numberOfRows: 9) for <SwiftUI.SwiftUIOutlineListView: 0x112087000> ( 0 CoreFoundation 0x000000018c834ec0 __exceptionPreprocess + 176 1 libobjc.A.dylib 0x000000018c31acd8 objc_exception_throw + 88 2 AppKit 0x00000001903eb304 -[NSTableRowData _availableRowViewWhileUpdatingAtRow:] + 0 3 SwiftUI 0x00000001bb365224 $s7SwiftUI0A17UIOutlineListViewC11removeItems2at8inParent13withAnimationy10Foundation8IndexSetV_ypSgSo07NSTableeL7OptionsVtF + 1388 4 SwiftUI 0x00000001bb3656f8 $s7SwiftUI0A17UIOutlineListViewC11removeItems2at8inParent13withAnimationy10Foundation8IndexSetV_ypSgSo07NSTableeL7OptionsVtFTo + 252 5 CoreFoundation 0x000000018c7a28b4 __invoking___ + 148 6 CoreFoundation 0x000000018c7a272c -[NSInvocation invoke] + 428 7 CoreFoundation 0x000000018c7d7958 -[NSInvocation invokeWithTarget:] + 64 8 AppKit 0x000000019052a110 -[_NSObjectAnimator forwardInvocation:] + 1512 9 CoreFoundation 0x000000018c7a0ee4 ___forwarding___ + 964 10 CoreFoundation 0x000000018c7a0a60 _CF_forwarding_prep_0 + 96 11 SwiftUI 0x00000001bb37e458 $s7SwiftUI22OutlineListCoordinatorC19recursivelyDiffRows_4with2by9expandAllyAA0a9UIOutlineD4ViewC_AA0dC4ItemCAA0nD4TreeVAA14ExpansionStateOtF + 37012 12 SwiftUI 0x00000001bb3802b8 $s7SwiftUI22OutlineListCoordinatorC19recursivelyDiffRows_4with2by9expandAllyAA0a9UIOutlineD4ViewC_AA0dC4ItemCAA0nD4TreeVAA14ExpansionStateOtF + 44788 13 SwiftUI 0x00000001bb37538c $s7SwiftUI22OutlineListCoordinatorC8diffRows2of2toyAA0a9UIOutlineD4ViewC_AA0kD4TreeVtF + 120 14 SwiftUI 0x00000001bb36f3d4 $s7SwiftUI22OutlineListCoordinatorC6update4diff04viewD4Tree18idSelectionChanged010navigationk4SeedL0011templateRowL011transactionySb_AA04ViewdI0VS3bAA11TransactionVtFyycfU_yyXEfU_ + 220 15 SwiftUI 0x00000001bb369044 $s7SwiftUI22OutlineListCoordinatorC24withSelectionUpdateGuard33_BE7B171B0BEE2A9E27ED12968C3771F8LLyySS_yyXEtF + 1320 16 SwiftUI 0x00000001bb36f2d4 $s7SwiftUI22OutlineListCoordinatorC6update4diff04viewD4Tree18idSelectionChanged010navigationk4SeedL0011templateRowL011transactionySb_AA04ViewdI0VS3bAA11TransactionVtFyycfU_ + 708 17 SwiftUICore 0x00000002277cb578 $sIeg_ytIegr_TRTA + 28 18 SwiftUICore 0x0000000227a6d2b8 $s7SwiftUI6UpdateO15dispatchActionsyyFZ + 1236 19 SwiftUICore 0x0000000227a6c79c $s7SwiftUI6UpdateO3endyyFZ + 212 20 SwiftUICore 0x0000000227f6061c $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_Tf4ddd_n + 176 21 CoreFoundation 0x000000018c7c17a8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36 22 CoreFoundation 0x000000018c7c1694 __CFRunLoopDoObservers + 552 23 CoreFoundation 0x000000018c7c0380 CFRunLoopRunSpecific + 648 24 HIToolbox 0x0000000197c000cc RunCurrentEventLoopInMode + 292 25 HIToolbox 0x0000000197c05d1c ReceiveNextEventCommon + 220 26 HIToolbox 0x0000000197c06020 _BlockUntilNextEventMatchingListInModeWithFilter + 76 27 AppKit 0x0000000190303650 _DPSNextEvent + 660 28 AppKit 0x0000000190c2a408 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688 29 AppKit 0x00000001902f675c -[NSApplication run] + 480 30 AppKit 0x00000001902cd02c NSApplicationMain + 888 31 SwiftUI 0x00000001ba77045c $s7SwiftUI6runAppys5NeverOSo21NSApplicationDelegate_So11NSResponderCXcFTf4e_nAA07TestingdG0C_Tg5Tm + 160 32 SwiftUI 0x00000001babf4854 $s7SwiftUI6runAppys5NeverOxAA0D0RzlF + 84 33 SwiftUI 0x00000001baf04134 $s7SwiftUI3AppPAAE4mainyyFZ + 224 34 Marlo SystemManagement.debug.dylib 0x0000000104c5e108 $s22Marlo_SystemManagement0a1_bC3AppV5$mainyyFZ + 40 35 Marlo SystemManagement.debug.dylib 0x0000000104c5e1d4 __debug_main_executable_dylib_entry_point + 12 36 dyld 0x000000018c358274 start + 2840 ) FAULT: NSTableViewException: Row index -1 out of row range (numberOfRows: 9) for <SwiftUI.SwiftUIOutlineListView: 0x112087000>; (user info absent)
分析与修复方案
这是macOS 15.1系统中SwiftUI框架的原生bug,根源是List/Table在处理层级条目折叠时,行索引计算逻辑异常,导致请求了不存在的-1行。
临时修复方式
- 替换组件嵌套逻辑:放弃使用
List的层级初始化器,改用OutlineGroup嵌套普通List,绕开系统有问题的层级实现:
var body: some View { List { OutlineGroup(fileHierarchyData, children: \.children) { item in Text(item.description) } } }
- 调整子项数据格式:将空数组类型的
children: []替换为children: nil,规避系统对空数组子项的折叠逻辑漏洞:
// 修改FileItem的description判断逻辑 var description: String { if children == nil || children?.isEmpty == true { return "📂 \(name)" } else { return "📁 \(name)" } } // 初始化时把空数组子项改为nil FileItem(name: "Documents", children: nil)
- 等待系统更新:这类UI框架bug通常会在后续macOS版本中修复,可等待苹果推送15.1的补丁或升级到更高版本系统。
内容的提问来源于stack exchange,提问作者user26880517
相关产品推荐
相关产品推荐

