Post

Replies

Boosts

Views

Activity

Reply to ForEach Breaks Data Binding
UPDATE - right after posting this I found a solution (See adding HStack comments at the end of discussion):https://forums.developer.apple.com/message/412141#412141So, this code works and I have no idea why:ForEach(noise.twoControlEffects.indices){ i in VStack{ if(self.noise.twoControlEffects[i].isDisplayed){ TwoControlTemplate(title: "Low Pass Filter", isBypassed: self.$noise.twoControlEffects[i].isBypassed, knobModel1: self.$noise.twoControlEffects[i].control1, knobModel2: self.$noise.twoControlEffects[i].control2) } } }Apparently, you just have to wrap the inside of your ForEach code in a HStack or VStack.
Apr ’20
Reply to Views do not update inside the ForEach in SwiftUI
I have the same problem with my array of objects. I can create my view using the index of zero - array[0] - and it responds to the databinding appropriately. If I wrap it with a ForEach but do not change the index of zero to an iterator - array[0] NOT array[index], it still creates the view, but now the data binding does not work. Same exact code, just wrapped in a ForEach loop that should only loop once (the array only has one object inside). What does the ForEach loop do that breaks databinding?// Does data bind correctly if(self.noise.twoControlEffects[0].isDisplayed){ TwoControlTemplate(title: "Low Pass Filter", isBypassed: self.$noise.twoControlEffects[0].isBypassed, knobModel1: self.$noise.twoControlEffects[0].control1, knobModel2: self.$noise.twoControlEffects[0].control2) } // Does not data bind correctly ForEach(noise.twoControlEffects.indices){ index in if(self.noise.twoControlEffects[0].isDisplayed){ TwoControlTemplate(title: "Low Pass Filter", isBypassed: self.$noise.twoControlEffects[0].isBypassed, knobModel1: self.$noise.twoControlEffects[0].control1, knobModel2: self.$noise.twoControlEffects[0].control2) } }I tried it with array[index] as well, but then did the above once it was not working.// Does not data bind correctly ForEach(noise.twoControlEffects.indices){ index in if(self.noise.twoControlEffects[index].isDisplayed){ Spacer() TwoControlTemplate(title: "Low Pass Filter", isBypassed: self.$noise.twoControlEffects[index].isBypassed, knobModel1: self.$noise.twoControlEffects[index].control1, knobModel2: self.$noise.twoControlEffects[index].control2) } }UPDATE - I found a simple solution that worked for me. It may work for you too.See my post:https://forums.developer.apple.com/thread/131577
Apr ’20