I am building a Eureka form and would like to put a loop within the form to build a list of steppers based on an array.
The code I am trying to use is:
let itemNames = ["one","two","three"]
// Eureka From Set-up
form
+++ Section("Select item values")
for itemName in itemNames{
<<< StepperRow() {
$0.tag = itemName
$0.title = itemName
$0.value = 0
}
}
However, when I do this I get an error on the StepperRow line that says:
Unary operator cannot be separated from its operand
So it looks as though Swift no longer thinks it is within the form and is looking at the < symbol as less than, rather than the row declaration.
Any thoughts on how to get around this?
The <<<
is a binary operator, which expects two operands (lhs <<< rhs
), whereas in your example above, you only supply it one (<<< operand
).
It's not possible to "pipe" each pass of a for
loop such as if each pass was a rhs
to be used with a lhs
operand outside of the scope of the loop (with lhs
for first pass being the result of form +++ Section(...)
). You could, however, make use of reduce
to achieve such functionality. Now, I haven't tested this with Eureka forms (however on dummy structures and operators), but it should look something like the following: (based on the +++
and <<<
operator functions declared in Eureka/Source/Core/Operators.swift)
form
+++ itemNames.reduce(Section("Select item values")) { (section, itemName) in
section
<<< StepperRow() {
$0.tag = itemName
$0.title = itemName
$0.value = 0
}
}
Alternative answer:
let itemNames = ["one","two","three"]
// Eureka From Set-up
form
+++ Section("Select item values")
for itemName in itemNames{
form.last
<<< StepperRow() {
$0.tag = itemName
$0.title = itemName
$0.value = 0
}
}
You just have to add "form.last" inside the for.loop.
//You can use following approach :-
let itemNames = ["one","two","three"]
let section = Section("Select item values")
form +++ section
// Eureka From Set-up
for itemName in itemNames{
section <<< StepperRow() {
$0.tag = itemName
$0.title = itemName
$0.value = 0
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With