Search code examples
swifteureka-forms

How can I get MultivaluedSection values from Eureka?


I've searched and cannot find an answer that addresses this particular issue.

I'm using Eureka in Swift 4.1 and want to store these values into another array.

How can I get the values selected here?

MultivaluedSection(multivaluedOptions: [.Insert, .Delete],
                           header: "Primary Instrument") {
            $0.tag = "instruments"
            $0.multivaluedRowToInsertAt = { _ in
                ActionSheetRow<String> {
                    $0.title = "Tap to Select"
                    $0.options = ["Vocals", "Guitar", "Bass", "Keyboards", "Ukelele"]
                }
            }
            $0 <<< ActionSheetRow<String> {
                $0.title = "Tap to Select"
                $0.options = ["Vocals", "Guitar", "Bass", "Keyboards", "Ukelele"]
            }
        }

The following method does not work for MultivaluedSection:

if let items = self.form.rowBy(tag: "instruments") as? MultivaluedSection
            {
                print(items.values())
            }

Solution

  • I always did it like this:

    let values: [String]? = (form.sectionBy(tag: "instructions")?.flatMap { ($0 as? ActionSheetRow<String>)?.value })
    

    Basically,

    • get the section with the tag
    • try to convert each row into a ActionSheetRow<String>
    • map each row that can be converted to its value.