SwiftUI 编译时好时不好的错误

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

编译时好时不好的原代码:

ForEach(viewModel.lists[index], id: \.self) { item in
                                    if let ship = item {
                                        ownShipPalletCell(ship: ship) {
                                            navigationManager.wrappedValue.pushView(tag: "RootPage") {
                                                ShipPalletDetailPage(ship: ship, fromOwn: true)
                                            }
                                        } share: {
                                            
                                        }

                                    }
                                }

修改后编译成功的代码:

let list: [ShipPallet?] = viewModel.lists[index]
                                ForEach(list, id: \.?.id) { item in
                                    if let ship = item {
                                        ownShipPalletCell(ship: ship) {
                                            navigationManager.wrappedValue.pushView(tag: "RootPage") {
                                                ShipPalletDetailPage(ship: ship, fromOwn: true)
                                            }
                                        } share: {
                                            
                                        }

                                    }
                                }

分析:应该是foreach 有时候不知道数据源问题,需要明确告诉编译器数据源和类型以及KeyPath<Data.Element, ID>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容