繁体   English   中英

SwiftUI 在 ForEach 循环中崩溃

[英]SwiftUI Crash in ForEach Loop

 ScrollView(.horizontal, showsIndicators: false, content: {
        LazyHGrid(rows: gridLayout2, alignment: .center, spacing: columnSpacing, pinnedViews: [], content: {
            Section(
                header: SectionView(rotateClockwise: false, headerText: "Premium Store"),
                footer: SectionView(rotateClockwise: true, headerText: "Premium Store")) {
                ForEach(premiumStoreItems) { item in
                    StoreItemView(storeItem: item)
                }
            }
        }) //: LAZYHGRID
        .frame(height: 200)
        .padding(15)
    })

崩溃发生在 ForEach 循环中。

如果我评论 ForEach 并从我的数组中传入 1 个特定项目,它工作正常。 但是我需要在我的数组中显示我的所有项目。

我不确定在这里做什么。

以下代码工作正常。

 //ForEach(premiumStoreItems) { item in
      StoreItemView(storeItem: premiumStoreItems[0])
 //}

崩溃日志(对不起,我不知道从哪里得到它)

Application Specific Information:
Fatal error: each layout item may only occur once: file SwiftUI, line 0

------ 完整的崩溃日志

进程:AppExample001 [3525] 路径:/Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/C93C2BE8-C23A-4813-9D6C-110E87786FFE/data/Containers/Bundle/Application/40562FF7-7B66-4BE4- 915A-5A2A35AD3EC2/AppExample001.app/AppExample001 标识符:AppExample001 版本:1.0 (1) 代码类型:X86-64 (Native) 父进程:launchd_sim [2488] 负责人:SimulatorTrampoline [611] 用户 ID:501

日期/时间:2021-04-07 12:23:50.173 -0400 操作系统版本:macOS 11.2.3 (20D91) 报告版本:12 Bridge 操作系统版本:5.2 (18P4347) 匿名 UUID:5A68DF80-DD1B-1132-4597-BBC26C334627

自启动以来的唤醒时间:7000 秒

系统完整性保护:启用

崩溃的线程:0 调度队列:com.apple.main-thread

异常类型:EXC_BAD_INSTRUCTION (SIGILL) 异常代码:0x0000000000000001、0x0000000000000000 异常说明:EXC_CORPSE_NOTIFY

终止信号:非法指令:4 终止原因:命名空间信号,代码 0x4 终止进程:exc 处理程序 [3525]

ID Vend/Dev 95f5 687f1002 秒前 ID 类型 6900.0 95f5 附加

应用程序特定信息:致命错误:每个布局项只能出现一次:文件 SwiftUI,第 0 行

CoreSimulator 732.18.6 - 设备:iPhone SE(第 2 代)(C93C2BE8-C23A-4813-9D6C-110E87786FFE) - 运行时:iOS 14.4(18D46) - 设备类型:iPhone SE(第 2 代)

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libswiftCore.dylib 0x00007fff2fc85004 assertionFailure( : :file:line:flags:) + 532 1 com.apple.SwiftUI 0x00007fff572e9d24 ViewCache.commitPlacedChildren(from:to:) + 4020 2 com.apple.SwiftUI 0x00007fff571a1f16 specialized IncrementalChildPlacements.updateValue() + 1558 3 com.apple.SwiftUI 0x00007fff572be37f partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init( : ) + 15 4 com.apple.AttributeGraph 0x00007fff4cd78723 AG::Graph::UpdateStack::update() + 505 5 com.apple.AttributeGraph 0x00007fff4cd78bb9 AG::Graph::update_attribute(AG::data::ptr<AG::节点>, bool) + 335 6 com.apple .AttributeGraph 0x00007fff4cd7d85f AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 523 7 com.apple.AttributeGraph 0x00007fff4cd8edc5 AGGraphGetValue + 203 8 com.apple.SwiftUI 0x00007fff572edac0 IncrementalPreference.children.getter + 48 9 com.apple.SwiftUI 0x00007fff572edb95 IncrementalPreference.value.getter + 85 10 com.apple.SwiftUI 0x00007fff572ede1c protocol witness for Rule.value.getter in conformance IncrementalPreference + 28 11 com.apple.AttributeGraph 0x00007fff4cd91907 dispatch thunk of Rule.value.getter + 7 12 com.apple.SwiftUI 0x00007fff570e9a18 implicit closure #2 in implicit closure #1 in closure #1 in clo sure #1 in Attribute.init(_:) + 56 13 com.apple.AttributeGraph 0x00007fff4cd78723 AG::Graph::UpdateStack::update() + 505 14 com.apple.AttributeGraph 0x00007fff4cd78bb9 AG::Graph::update_attribute(AG ::data::ptr<AG::Node>, bool) + 335 15 com.apple.AttributeGraph 0x00007fff4cd7d318 AG::Graph::value_ref(AG::AttributeID, AGSwiftMetadata const*, bool*) + 130 16 com.ADApple1C50D4B1 .AttributeGraph 0x00007fff4cd8ee13 AGGraphGetValue + 281 17 com.apple.SwiftUI 0x00007fff57987807 GraphHost.updatePreferences() + 39 18 com.apple.SwiftUI 0x00007fff57458cbf ViewGraph.updateOutputs(at:) + 95 19 com.apple.SwiftUI 0x00007fff573f04e4 specialized closure #1 in ViewRendererHost.渲染(间隔:更新显示列表 :) + 1316 20 com.apple.SwiftUI 0x00007fff573ed28e specialized ViewRendererHost.render(interval:updateDisplayList:) + 366 21 com.apple.SwiftUI 0x00007fff57a59c0e specialized UIHostingController._render(seconds:) + 46 22 com.apple.dt.XCPreviewKit 0x000000010c117536 0x10c10a000 + 54582 23 com. apple.dt.XCPreviewKit 0x000000010c1246eb 0x10c10a000 + 108267 24 com.apple.dt.XCPreviewKit 0x000000010c11b91f 0x10c10a000 + 71967 25 com.apple.dt.XCPreviewKit 0x000000010c11bc08 0x10c10a000 + 72712 26 com.apple.dt.XCPreviewKit 0x000000010c11ddff 0x10c10a000 + 81407 27 com.apple. dt.XCPreviewKit 0x000000010c11c274 0x10c10a000 + 74356 28 com.apple.dt.XCPreviewKit 0x000000010c1134e3 0x10c10 a000 + 38115 29 com.apple.dt.XCPreviewKit 0x000000010c112eee 0x10c10a000 + 36590 30 libdispatch.dylib 0x00007fff2010532f _dispatch_call_block_and_release + 12 31 libdispatch.dylib 0x00007fff20106508 _dispatch_client_callout + 8 32 libdispatch.dylib 0x00007fff20112ff7 dispatch_main_queue_callback_4CF + 1045 33 com.apple.CoreFoundation 0x00007fff2038fdbb CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9 34 com.apple.CoreFoundation 0x00007fff2038a63e __CFRunLoopRun + 2685 35 com.apple.CoreFoundation 0x00007fff203896d6 CFRunLoopRunSpecific + 567 36 com.apple.GraphicsServices 0x00007fff2c257db3 GSEventRunModal + 139 37 com.apple.UIKitCore 0x00007fff24696cf7 -[UIApplication run] + 912 38 com.apple.UIKitCore 0x00007fff2469bba8 UIApplicationMain + 101 39 Z4D236D9A2D102C5FE6AD1C50 DA4BEC50Z.apple.SwiftUI 0x00007fff5791abc7 closure #1 in KitRendererCommon( :) + 119 40 com.apple.SwiftUI 0x00007fff5791ab3f runApp( :) + 143 41 com.apple.SwiftUI 0x00007fff5745212d static App.main() + 61 42 com.oneorangetree.AppExample001 0x000000010bfcaf4e static AppExample001App.$main() + 78 43 com.oneorangetree.AppExample001 0x000000010bfcafd4 main + 20 44 libdyld.dylib 0x00007fff2025a3e9 start + 1

ForEach 无法唯一识别每个项目,请确保您的数据 model 结构已确认可识别协议

试试这个

ForEach(premiumStoreItems, id:\.self)
ForEach(premiumStoreItems, id:\.id) // if you've unique identifier with the name of "id"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM