Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop within Swift Eureka Form

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?

like image 454
Michael Moulsdale Avatar asked Nov 19 '16 16:11

Michael Moulsdale


3 Answers

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
            }
    }
like image 162
dfrib Avatar answered Oct 08 '22 16:10

dfrib


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.

like image 45
Skyborg Avatar answered Oct 08 '22 16:10

Skyborg


  //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
        }
    }
like image 2
Aakash Verma Avatar answered Oct 08 '22 16:10

Aakash Verma