'ForEach Loop on Properties of different objects but of same type
This is the problem function:
func setStateForAudioIOControls(enabled: Bool) {
let finalColor = enabled ? audioIOView.enabledColor : audioIOView.disabledColor
[audioIOView.enableButton.configuration?.background.backgroundColor, audioIOView.disableButton.configuration?.background.backgroundColor].forEach { backgroundColor in
backgroundColor = finalColor
}
}
When I try to implement a ForEach loop on the UIColor properties in this array I get the error: Cannot convert value of type '(inout UIColor?) -> ()' to expected argument type '(UIColor?) throws -> Void
However, when I change the function to:
func setStateForAudioIOControls(enabled: Bool) {
let finalColor = enabled ? audioIOView.enabledColor : audioIOView.disabledColor
audioIOView.slider.tintColor = finalColor
[audioIOView.enableButton, audioIOView.disableButton].forEach { button in
button.configuration?.background.backgroundColor = finalColor
}
}
The error goes away and the color value changes successfully. I want to implement the function as the top one (where I'm loop directly over an array of colors) so that I can use this ForEach on different classes (e.g. changing the slider tint color). Any idea why the first one doesn't work but the second one does? They ultimately are trying to do the same thing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
