V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sunshinev
V2EX  ›  程序员

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

  •  
  •   sunshinev ·
    sunshinev · 19 天前 · 895 次点击
        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>'

    请问这是什么原因呀?

    第 1 条附言  ·  17 天前

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

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

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

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

    ```
    ForEach(detailViewModel.videoDetail.ResourceModuleList) { item in
    ...
    ForEach(item.links) { a in
    .....
    }
    }
    ```
    再试试呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2819 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:27 · PVG 08:27 · LAX 17:27 · JFK 20:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.