I am new to Swift and I am trying to experiment with the language to familiarize myself with all the cool functionality. Currently, I am trying to write a forEach
closure to append a different string depending on the value of the element in the forEach
. The variable lightStates
is a [Bool]
list. What I am looking for is for statusStr
to be a string of "1"
's and "0"
's depending if the b
element in the forEach
closure is true or false,
I am getting a "Declared closure result 'String' is incompatible with contextual type 'Void'"
error at the String in
... line within the closure.
var statusStr : String = ""
statusStr.append(lightStates.forEach{ (b:Bool) -> String in return b ? "1":"0"})
return "Lights: \(statusStr) \n"
Ideally, I would like to know know if what I am doing is even allow/possible. But also I am open to suggestions in how to get the desired functionality (printing a string of "1"
's and "0"
's based on a array of [Bool]
s)