Good morning everyone,I'm trying to create an application for a connected home. So I have a light class that has the property of having a @Published var allLights: [Light] as follows:struct Light: Identifiable {
var id: Int
var deviceName: String
var intensity: Int
//I don't know if I have to remove the @State wrapper because I'll use Toggle to toggle this var...
@State var mode: Bool
}
class Lights: ObservableObject {
@Published var allLights = [Light]()
init() // I initialize this array using an API (thanks to SwiftyJSON)
}Thus, in a View I would like to be able to change the different values of my lights (only the mode var for the moment). However the Toggle does not change the value of the @State var mode of each light... Here is the code for the view:struct LightsCollection: View {
@EnvironmentObject var lightStore : Lights
var body: some View {
VStack {
ForEach(lightStore.allLights) { light in
HStack {
Text("\(light.id)")
Text("\(light.deviceName)")
Text("- \(light.intensity)")
Text("- " + String(light.mode))
Toggle(isOn: light.$mode) {
Text(light.mode ? "Switch off" : "Switch on")
}
}
}
}
}
}I've already read a lot of forums but nothing to do... Any help will be appreciated :-)
Post
Replies
Boosts
Views
Activity
Morning, everyone,
I'm developing a widget using an API that sends an array of different sizes depending on the size of the widget (small : 3 news, medium: 6 news, large: 9 news).
I have the impression that there is a problem between the @Environment(\.widgetFamily) var and the timeline function (where I call the API) of TimelineProvider.
Indeed in this function the environment variable is always equal to the systemMedium size despite the "real" size of the widget.
Do you also have the same problem on your side ?
Is it a known bug from Apple ?
How can I solve this bug ?
Thanks for your help :]