Eureka: Use Dictionary for '$0.options'

Created on 21 Aug 2016  路  8Comments  路  Source: xmartlabs/Eureka

I would like to use a dictionary instead of a string for the options, is that possible

Example
$0.options = ["1":"High", "2":"Medium", "3":"Low", "4":"None"]

SelectorRow awaiting response question

Most helpful comment

It might be helpful for you to describe what you want to accomplish at a higher level.

If you're simply trying to get some kind of other associated value based on what your users select, I would recommend creating an enum where the rawValue represents your dictionary keys and the description represents your values.

enum Magnitude: Int, CustomStringConvertible {
    case High = 1
    case Medium = 2
    case Low = 3
    case None = 4

    static let allMagnitudes = [High, Medium, Low, None]

    var description: String {
        switch self {
        case High:
            return "High"
        case Medium:
            return "Medium
        case Low:
            return "Low"
        case None:
            return "None"
        }
    }
}

That would allow you to have something like

<<< SegmentedRow<Magnitude>() {
    $0.options = Magnitude.allMagnitudes
    $0.value = Magnitude.Medium
}

All 8 comments

It might be helpful for you to describe what you want to accomplish at a higher level.

If you're simply trying to get some kind of other associated value based on what your users select, I would recommend creating an enum where the rawValue represents your dictionary keys and the description represents your values.

enum Magnitude: Int, CustomStringConvertible {
    case High = 1
    case Medium = 2
    case Low = 3
    case None = 4

    static let allMagnitudes = [High, Medium, Low, None]

    var description: String {
        switch self {
        case High:
            return "High"
        case Medium:
            return "Medium
        case Low:
            return "Low"
        case None:
            return "None"
        }
    }
}

That would allow you to have something like

<<< SegmentedRow<Magnitude>() {
    $0.options = Magnitude.allMagnitudes
    $0.value = Magnitude.Medium
}

@lognaturel Thanks for helping us support eureka 馃憤 馃幈

Apart of all that, most of the time we want to show an ordered option list and dictionaries don't have any particular order.

Thanks for your reply. What I am after is similar to the following from another swift form class. Notice how it allows you to first set the options, but it then allows you to replace the numbers for text but only for display so when you post the form the number is sent and also you set the value using a number and not the display name

` row.configuration.selection.options = [0, 1, 2, 3]

    row.configuration.selection.optionTitleClosure = { value in
        guard let option = value as? Int else { return "" }

        switch option {
        case 0:
            return "None"
        case 1:
            return "High"
        case 2:
            return "Medium"
        case 3:
            return "Low"
        default:
            return ""
        }
    }`

Great! That enum suggestion should work well. When you read the row's value, you can cast it as Magnitude (or whatever you call it) and use .rawValue to get the number or .description to get the text.

@wstudios Could you implement this? I believe the best solution is to use an enum as @lognaturel suggested.

For dynamically generated option lists, this is a bit problematic. Any ideas on how to workaround that scenario?

In my case, I need to associate an ID with the string that is displayed.

I was able to solve by problem by discovering this solution - https://github.com/xmartlabs/Eureka/issues/495

@lognaturel How do I cast it?

I have a enum like this:

enum Audience: Int, CustomStringConvertible {
    case PUB = 1
    case OMF = 2
    case JME = 3

    static let allOptions = [PUB, OMF, JME]

    var description: String {
        switch self {
        case .PUB:
            return "Public"
        case .OMF:
            return "Only My Friends"
        case .JME:
            return "Just Me"
        }
    }
}

I'm using like this:

<<< ActionSheetRow<Audience>("permission") {
    $0.title = "Available for";
    $0.selectorTitle = "Available for";
    $0.options = Audience.allOptions
    $0.value = Audience.PUB
}

I'm getting value:

    let permission: ActionSheetRow<Audience>? = form.rowBy(tag: "permission");
    let permv = permission?.value;

It's return:

Public

I need (for db, because PUB is "another word" for another language)

PUB

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tomas1405 picture Tomas1405  路  3Comments

fpena picture fpena  路  3Comments

Isuru-Nanayakkara picture Isuru-Nanayakkara  路  3Comments

Tomas1405 picture Tomas1405  路  3Comments

annjawn picture annjawn  路  3Comments