• 请不要在回答技术问题时复制粘贴 AI 生成的内容
sunshinev
V2EX  ›  程序员

swiftui 嵌套 foreach 报错,请教大神帮忙看看

  •  
  •   sunshinev ·
    sunshinev · Apr 10, 2024 · 2673 views
    This topic created in 801 days ago, the information mentioned may be changed or developed.
        var playResource: some View {
            LazyVStack(alignment: .leading) {
                
                ForEach(detailViewModel.videoDetail.ResourceModuleList, id: \.id) { item in
                    LazyVStack {
                        Text(item.title)
                        ForEach(item.links, id:\.id) { a in
                            LazyHStack {
                                Button {
                                    
                                } label: {
                                    Text(a.title)
                                }
                            }
                        }
                    }
                }
            }
        }
    

    xx

    报错文案: Cannot convert value of type '[ResourceLink]' to expected argument type 'Binding<C>'

    请问这是什么原因呀?

    Supplement 1  ·  Apr 12, 2024

    谢谢各位大佬 我试了下,好像是Button的label中使用了Text的问题导致的,

    Button {
        
    } label: {
        Text(a.title)
    }
    

    xcode的报错提醒的地方,往往不是出错的根本源头。。。这个很坑

    3 replies    2024-04-11 10:29:53 +08:00
    lx01xsz
        1
    lx01xsz  
       Apr 10, 2024
    缺少上下文啊, 报错的源头貌似不是这
    ma46
        2
    ma46  
       Apr 11, 2024
    是要用 binding 类型吧, 就是那个美元符号的
    Mephisto233
        3
    Mephisto233  
       Apr 11, 2024
    缺少上下文,看不出来,或者你把 detailViewModel.videoDetail.ResourceModuleList 和 item.links 里面的模型实现 Identifiable 协议 ,然后代码改成:

    ```
    ForEach(detailViewModel.videoDetail.ResourceModuleList) { item in
    ...
    ForEach(item.links) { a in
    .....
    }
    }
    ```
    再试试呢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2503 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 11:13 · PVG 19:13 · LAX 04:13 · JFK 07:13
    ♥ Do have faith in what you're doing.