核心数据+ SwiftUI:无法将类型“布尔"的值转换为预期的参数类型“绑定<布尔>" [英] Core Data + SwiftUI: Cannot convert value of type 'Bool' to expected argument type 'Binding<Bool>'

查看:84
本文介绍了核心数据+ SwiftUI:无法将类型“布尔"的值转换为预期的参数类型“绑定<布尔>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些带有切换的列表.我从核心数据实体获得的列表数据.

I need to have some list with toggles. Data for list I’ve got from Core Data entity.

这是我的代码

// get array products from Core Data
@FetchRequest(fetchRequest: Products.getAllProducts()) var myProducts: FetchedResults<Products>

// here I try to show List
List(myProducts.indices) { index in
    Toggle(isOn: self.myProducts[index].productStatus) {
        Text(self.myProducts[index].productName)
        }
}

属性isOn给我错误:无法将'布尔'类型的值转换为预期的参数类型'绑定'

Attribute isOn give me error: Cannot convert value of type 'Bool' to expected argument type 'Binding'

需要做什么?

推荐答案

我为我的案件找到了解决方案

I found solution for my case

ForEach(myProducts.indices){ index in
    Toggle(isOn:
        Binding<Bool>
         (get: {return self.myProducts[index].productStatus},
          set: { p in self.myProducts[index].productStatus = p}
         )) {
              Text(self.myProducts[index].productName)
         }
         .onTapGesture {
              self.myProducts[index].productStatus.toggle()
              do{
                  try self.managedObjectContext.save()
              }catch{
                  print(error)
              }
        }
 }

只有我不懂一行代码:设置:{p in self.myProducts [index] .productStatus = p}

Only I don't understand about one line of code: set: { p in self.myProducts[index].productStatus = p}

有人可以向我解释吗?

这篇关于核心数据+ SwiftUI:无法将类型“布尔"的值转换为预期的参数类型“绑定&lt;布尔&gt;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆