7

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?

Michael Moulsdale
  • 1,488
  • 13
  • 34

3 Answers3

5

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
            }
    }
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • I understand that. <<< is a specific syntax for Eureka forms and so when not within a For loop works correctly. My question is mote about Eureka and putting a loop within a Eureka Form. The space is required as part of that Syntax – Michael Moulsdale Nov 19 '16 at 16:19
  • @MichaelMoulsdale updated my answer (untested for Eureka framework, but tested on dummy types and operators). – dfrib Nov 19 '16 at 17:03
  • 1
    Awesome, was really sceptical of the answer but it worked a treat. @xmartlabs, you guys should really add this to the documentation – Michael Moulsdale Nov 20 '16 at 10:35
  • @MichaelMoulsdale good to hear that it worked also in practice with Eureka. Happy to help! – dfrib Nov 20 '16 at 11:20
4

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.

Skyborg
  • 854
  • 13
  • 13
2
  //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
        }
    }
Aakash Verma
  • 91
  • 1
  • 2
  • 5