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

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

  •  
  •   sunshinev ·
    sunshinev · 29 天前 · 1161 次点击

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

        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 条附言  ·  28 天前

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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

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

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

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

    ```
    ForEach(detailViewModel.videoDetail.ResourceModuleList) { item in
    ...
    ForEach(item.links) { a in
    .....
    }
    }
    ```
    再试试呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4486 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:32 · PVG 13:32 · LAX 22:32 · JFK 01:32
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com