如何在SwiftUI中的foreach循环中设置切换状态 [英] How do I set the toggle state in a foreach loop in SwiftUI

查看:292
本文介绍了如何在SwiftUI中的foreach循环中设置切换状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在字典的值循环中设置显示切换时,错误消息给我的帮助很少.

When I try to set show a toggle inside a loop of value from a dictionary, I get very little help from the error message.

如果我取消注释下面三行注释的代码,尝试为循环中的每个属性添加一个切换开关,则会出现以下错误:

If I un-comment the 3 lines of commented code below, trying to add a toggle for each of the properties in the loop, I get the following error:

无法将类型'HStack,Text,ConditionalContent)>>'的值转换为结束结果类型'_'

Cannot convert value of type 'HStack, Text, ConditionalContent)>>' to closure result type '_'

import SwiftUI

struct properties {
    var id : Int
    var property : String
    var isOn : Bool
}

struct ContentView: View {

    @State var propertyValues: [properties] = [properties(id: 1, property: "DemoData", isOn: true),
                                                 properties(id: 2, property: "ShowLocalEvents", isOn: false)]

    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(propertyValues.identified(by: \.id)) { propertyValue in
                        HStack {
//                            Toggle(isOn: propertyValue.isOn) {
//                                Text("")
//                            }
                            Text("\(propertyValue.property)")
                            if propertyValue.isOn {
                                Text("On")
                            } else {
                                Text("Off")
                            }
                        }
                    }
                }
            }
        }
    }
}

推荐答案

此处的问题是,初始化程序Toggle(isOn:label:)为其isOn参数使用Binding<Bool>而不是仅仅Bool. Binding<_>是对属性的一种可读写的视图",它允许控件更新它不拥有的值,并将那些更改传播给拥有该属性的任何人.

The issue here is that the initializer Toggle(isOn:label:) takes a Binding<Bool> for its isOn parameter rather than just a Bool. A Binding<_> is sort of a readable-writable "view" into a property that allows a control to update a value that it doesn't own and have those changes propagate out to whoever does own the property.

我使它变得比所需的更为复杂.以下作品:

I made this more complicated than it needed to be. The following works:

ForEach($propertyValues.identified(by: \.id.value)) { (propertyValue: Binding<properties>) in
    HStack {
        Toggle(isOn: propertyValue.isOn) {
            Text("")
        }
        // ...
    }
}

通过使用$propertyValues,我们可以访问数组本身的Binding,该数组将转换为对每个元素的绑定.

By using a $propertyValues, we are accessing a Binding to the array itself, which is transformed into bindings to each of the elements.

要使上述方法起作用,您需要在正文中的几个位置添加.value,以便您引用的是实际值,而不是对值的绑定.

For the above to work, you'll need to add .value in a couple places in the body so that you are referring to the actual values and not the bindings to the values.

这篇关于如何在SwiftUI中的foreach循环中设置切换状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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