SwiftUI:无法将“Bool"类型的值转换为预期的参数类型“Binding<Bool>" [英] SwiftUI: Cannot convert value of type 'Bool' to expected argument type 'Binding<Bool>'
本文介绍了SwiftUI:无法将“Bool"类型的值转换为预期的参数类型“Binding<Bool>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为 SwiftUI 创建一个基于文本的游戏
Creating a text-based game for SwiftUI
想知道为什么我不能在字符 ForEach 循环中访问 isSelected?
Wondering why can't I access isSelected in character ForEach loop?
收到此错误:无法将Bool"类型的值转换为该行上的预期参数类型Binding"
Get this error: Cannot convert value of type 'Bool' to expected argument type 'Binding' on this line
Toggle(isOn: character.isSelected){
变量对象声明:@Binding var 字符:[字符]
代码在这里:
VStack {
ForEach(characters) { character in
HStack{
VStack(alignment:.leading) {
Text("\(character.name)")
.fontWeight(.bold)
Text("\(character.description)")
.lineLimit(10)
}
Spacer()
Toggle(isOn: character.isSelected){
Text("a")
}.labelsHidden()
推荐答案
你只需要添加一个 $:
You just need to add a $:
Toggle(isOn: $character.isSelected){
这篇关于SwiftUI:无法将“Bool"类型的值转换为预期的参数类型“Binding<Bool>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文