EurekaCommunity / SplitRow

A row for Eureka to put two rows side by side into the same UITableViewCell
MIT License
56 stars 27 forks source link

Parsing values from a SplitRow. #43

Closed primeviltom closed 4 years ago

primeviltom commented 4 years ago

Hi there,

I've decided to utilize splitRow in a project so some fields can be optional, so I am using splitRow with a <SwitchRow, DecimalRow> for example.

I want to parse the entire form when the user has submitted, however I'm having trouble getting data from the SplitRow's like I normally would in a non split-row.

My form looks like: https://i.imgur.com/jUS8lWg.png

When I print out the form data to the console, it looks like:

[
"postAs": Optional("The Byron List"), 
"postDescription": Optional("Test details"), 
"splitRowURL": Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.String>(left: Optional(true), right: Optional("Www.fun.com"))), 
"saveButton": nil, "title": Optional("Test Title"), 
"splitEvent": Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.String>(left: Optional(true), right: nil)), 
"splitRowPrice": Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.Double>(left: Optional(true), right: Optional(5.55))), 
"splitLocation": Optional(SplitRow.SplitRowValue<Swift.Bool, __C.CLLocation>(left: Optional(true), right: Optional(<-28.63381450,+153.63690625> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/19, 2:21:24 PM Australian Eastern Daylight Time)))]

and this code:

    private func parsePost(formData : [ String : Any? ]) -> [String : String] {
        let splitRowPrice = formData["splitRowPrice"]
            //returns nil: as? SplitRowValue<Bool,String>
        print(splitRowPrice)
        return ["hello" : "kitty"]
    }

will print out the splitRowPrice as: Optional(Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.Double>(left: Optional(true), right: Optional(242.34))))

I just don't know how exactly I get the the left and right values!! Casting as a SplitRow<SwitchRow, DecimalRow> and variations will give me a nil value, and I'm just not sure exactly how to get the values from the form. Please help!

Thanks.

marbetschar commented 4 years ago

Hi @primeviltom,

The SplitRowValue is actually a struct, containing the fields left and right which in turn contain the actual leftRow and rightRow values.

That said, you should be able to access the desired values with something like this (pseudo code):

let splitRowValue = xyz as? SplitRow<SwitchRow, DecimalRow>
// value of rowLeft:
print(splitRowValue?.left)
// value of rowRight:
print(splitRowValue?.right)

I'll close this issue for now. Feel free to reopen if you see fit.

EDIT: added ? after splitRowValue because its an Optional.

primeviltom commented 4 years ago

Thanks for your reply, however I'm still unable to get a value out. The auto-complete shows that I should be getting a Bool or Double from the value I'm after. i.e. https://i.imgur.com/th58ydP.png

However, this code:

        let splitRowValue = formValues["splitRowPrice"] as? SplitRow<SwitchRow, DecimalRow>
        print(splitRowValue?.rowLeft?.value)
        print(splitRowValue?.rowRight?.value)

Still generates nils in the console (formatted for readability):

[
"postDescription": Optional("Wef"), 
"splitRowURL": Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.String>(left: Optional(false), right: nil)), 
"splitRowPrice": Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.Double>(left: Optional(true), right: Optional(9.99))), 
"splitLocation": Optional(SplitRow.SplitRowValue<Swift.Bool, __C.CLLocation>(left: Optional(true), right: Optional(<-28.63381450,+153.63690625> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/17/19, 6:27:40 AM Australian Eastern Daylight Time))), 
"postAs": Optional("The Byron List"), 
"splitEvent": Optional(SplitRow.SplitRowValue<Swift.Bool, Swift.String>(left: Optional(false), right: nil)), 
"saveButton": nil, 
"title": Optional("Wef")
]
nil
nil
marbetschar commented 4 years ago

@primeviltom it should be splitRowValue?.left instead of splitRowValue?.rowLeft?.value - and same thing for the right value ;)

primeviltom commented 4 years ago

Thanks again for the reply. Using splitRowValue?.left gives me a Value of type 'SplitRow<SwitchRow, DecimalRow>' has no member 'left' error and I can't build: https://i.imgur.com/wysr9es.png

formValues comes from self?.form.values() when the user taps a ButtonRow. I'm not sure where I'm going wrong..

marbetschar commented 4 years ago

You have to levels of "values" coming into play:

  1. The Value of the SplitRow
  2. The values of the embedded rows.

The value type of the SplitRow is a struct of type SplitRowValue which contains a left and a right property, which do hold the values of the embedded rows.

To access the SplitRow's value you want to do something like this:

let splitRowValue = row.value // this returns a value of something like SplitRowValue<Bool, Double>

But to actually access the values of left and right row, you want to do something like this:

let leftValue = row.value?.left // Bool
let rightValue = row.value?.right // Double

If that still does not work, I suggest you'll have another look on how to retrieve a value from a row in the Eureka documentation.

UPDATE: as for your cast in your screenshot, you probably want to cast it to SplitRowValue instead of SplitRow, since form.values() actually returns all row.value fields of the form - which in case of SplitRow holds the struct SplitRowValue (see above).

UPDATE 2: Just seen my previous comment was a bit missleading, since it did not contain proper code. Seems like it was already too late for me 🤣 - however, this description here should be correct and get you going.

primeviltom commented 4 years ago

Finally got it, by casting to SplitRowValue, not SplitRow! Thank you very much 😀🤙