Search code examples
swiftswift4.2eureka-forms

Having issues retrieving values from a Multiple Selector Row


I've been trying to figure this one out all day and have been unsuccessful. What I'm trying to do is retrieve selected values from a multiple selector row. The row so far is fully functioning and I've been able to set it up so that my options are structs.

The problem I'm having is towards the end when I need to retrieve the data from the form. I'm able to use form.values() and know how to work with the dictionary for the most part.

When I call for the values in the multi-row, they come through as 'Sets'. I am still relatively new to Swift so I hadn't dealt with these before, but from what I've read so far they are collection types like Arrays?

this is the part that is giving me issues:

//
        <<< ButtonRow("btnnRow") { row in
            row.title = "Confirm selection."
            row.onCellSelection({ (cell, row) in
                let formValues = self.form.values()
                let koko = formValues["multiSelectTest"] as! Set<MultiTestStruct>
                switch koko.isEmpty {
                case true:
                    print("Set is empty")
                case false:
                    print("Set is NOT empty!")
                }
            })
    }

This is the latest iteration of what I've attempted. It's able to print 'empty' on first load, but as soon as I select an option and unselect it again, crash!

I think I'm having problems after selection because when I print all values, it's initially "multiSelectTest": nil, but after making a selection in the Multi-row then unselecting it, this is what it turns into: "multiSelectTest": Optional(Set([])).

Apologies if this makes no sense, I'd be happy to explain further if needed, it's currently 2am and my brain is pretty frazzled!

TL:DR - How to get values from a multiple selection row.

Thank you for reading.

Update: error message- Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value - I get this error if I try to get the rows value before selecting and unselecting. Once selection has been toggled, there is no issue.

This is the console output of all values prior to making a selection:

["btnRow": nil, "btnRow2": nil, "multiSelectTest": nil]

This is the console output after selecting an option:

["btnRow": nil, "btnRow2": nil, "multiSelectTest": Optional(Set([theDD_Admin.MultiTestStruct(name: "HELLO!")]))]

And finally, this is after I've cleared the multi-row of all options:

["btnRow": nil, "btnRow2": nil, "multiSelectTest": Optional(Set([]))]

My main plan of action was to call for the multi-row values if multi was nil but as you can see it is only nil whilst untouched. After making an option an even unselecting, it turns into a set and I get lost from there.

thanks


Solution

  • From @koropok's comment, this is what I've managed to come up with. In the multi-row, I set the .onChange behaviour -

    row.onChange({ (row) in
                    if row.value?.isEmpty == true {
                        // if row Set<> is empty then this row will be cleared and set to nil
                        self.form.setValues(["multiSelectTest" : nil])
                    }
                })
    

    I was running into an error when I tried to do row.value = nil and this way is working as intended.

    This is what I've come up with and is working well for me so far. For those with more experience than I, is this the correct/best approach? Would like to know if anyone would do anything differently.

    Thanks again!