Eureka: The value of picker rows should really be independent of what is displayed

Created on 30 Mar 2018  路  3Comments  路  Source: xmartlabs/Eureka

Let's say I have a picker row that allows people to select an option between "1/3", "2/3" and "all".

The three options represent Double values 0.333..., 0.666... and 1.0 respectively. I want to display the options as fractions as shown above, but in my code I want my row's value to be of type Double.

If I use a PickerRow<Double>, the numbers won't show up as fractions.

If I use a PickerRow<String>, I won't be able to access value as a Double.

I know I can set the displayValueFor property, but that means I need to write a function that converts 0.333... to "1/3" i.e. Double -> String.

Wouldn't it be easier to just write a function Int -> String that accepts the index of the option and returns what that option should display as? This way I don't need to compute the display value.

PickerRow enhancement

Most helpful comment

Yeah I know these two workarounds exist. This is kind of a feature request to add something like this:

var optionDisplayStringForIndex: (Int) -> String

or

var optionDisplayStrings: [String]

where each item of this array will be the corresponding display string for the option at the same index in the options array.

All 3 comments

Hi @Sweeper777, you can call row.options.index(of:) to create that function.

Otherwise you can have a struct conforming to Equatable with two variables, one being a String and one a Double. You would then create the row as PickerRow<YourStruct>. But you would also have to define displayValueFor to show the fraction of the object.

Yeah I know these two workarounds exist. This is kind of a feature request to add something like this:

var optionDisplayStringForIndex: (Int) -> String

or

var optionDisplayStrings: [String]

where each item of this array will be the corresponding display string for the option at the same index in the options array.

I've just submitted a pull request that adds the ability to do this for DoublePickerRows and TriplePickerRows.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pteasima picture pteasima  路  3Comments

zoul picture zoul  路  3Comments

sedwo picture sedwo  路  3Comments

Tomas1405 picture Tomas1405  路  3Comments

allanrojas picture allanrojas  路  3Comments