Eureka: PushRow with dynamic value ?

Created on 25 May 2016  路  8Comments  路  Source: xmartlabs/Eureka

Hello,

I have a little problem with PushRow.
I retrieves my values via JSON but I can't put them in the PushRow :/

My values :

{
"listValue": [{
    "id": 1,
    "value": "Value 1"
},{
    "id": 2,
    "value": "Value 2"
},{
    "id": 3,
    "value": "Value 3"
}]
}

In your example :

enum EventState {
        case Busy
        case Free

        static let allValues = [Busy, Free]
}

PushRow<EventState>("Show As") {
      $0.title = "Show As"
      $0.options = EventState.allValues
}

And I do not know how to add my data into a dynamically enum..
Do you have an idea to help me ?

Stack Overflow PushRow question

Most helpful comment

Solution :

struct MyStruct : CustomStringConvertible {

    var id: Int
    var value: String

    init(id: Int, value: String) {
        self.id = id
        self.value = value
    }

    var description: String {
        //return "\(self.id)"+" "+"\(self.value)"
        return "\(self.value)"
    }

}

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value
    return areEqual
}
var arrayOfMyStruct:[MyStruct] = []
for item in item["listValue"].arrayValue {
    print(item["id"].intValue)
    print(item["value"].stringValue)
    print("#############")
    arrayOfMyStruct.append(MyStruct(id: item["id"].intValue, value: item["value"].stringValue))
}

form.last! // recup猫re la derni猫re section
    <<< PushRow<MyStruct>(idItem) {
        $0.title = item["libItem"].stringValue
        $0.options = arrayOfMyStruct
    }

it's work !

But now, there is an another problem (with value selected) ==> #514

All 8 comments

Nobody can help me ? :/

What do you mean by dynamic enum?

I don't know if it's possible or not for the "dynamic enum".. maybe I should use another method ?

I would like put my value (retrieves by JSON) in the PushRow but i can't with this example :s

Do you have an idea ?

These are the steps to make it work,

  1. Declare a struct to hold the id and value, MyStruct.
  2. Make it conform Equatable.
  3. Use the struct as Generic PushRow type. PushRow<MyStruct>
  4. Make MyStruct conforms CustomStringConvertible by implementing var description : String { get }
  5. Set up push row options with an array of MyStruct. It's your responsibility to convert the json listValue response into an array of MyStruct. Should be super easy using CollectionType helpers like map and so on.

Please general questions should be posted in StackOverflow as contribution guidelines points out.

Thank you for you help, I really appreciate :)

I put my code here for the others peoples

1/ Declare a struct to hold the id and value :

struct MyStruct {
    var id: Int
    var value: String

    init(id: Int, value: String) {
        self.id = id
        self.value = value
    }
}

2/ Make it conform Equatable

struct MyStruct {
    var id: Int
    var value: String

    init(id: Int, value: String) {
        self.id = id
        self.value = value
    }
}

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value

    return areEqual
}

4/ Make MyStruct conforms CustomStringConvertible

struct MyStruct : CustomStringConvertible {
    var id: Int
    var value: String

    init(id: Int, value: String) {
        self.id = id
        self.value = value
    }

    var description: String {
        return "\(self.id)"+" "+"\(self.value)"
    }

}

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value

    return areEqual
}

5/ I have a problem to convert the json listValue response into an array of MyStruct..
How to do that?
I continue to seek and I will come post the solution..

Edit : My listValue is a JSON (with SwiftyJSON)

Hello,

I'm still on this problem, can you help me please? :/

Solution :

struct MyStruct : CustomStringConvertible {

    var id: Int
    var value: String

    init(id: Int, value: String) {
        self.id = id
        self.value = value
    }

    var description: String {
        //return "\(self.id)"+" "+"\(self.value)"
        return "\(self.value)"
    }

}

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value
    return areEqual
}
var arrayOfMyStruct:[MyStruct] = []
for item in item["listValue"].arrayValue {
    print(item["id"].intValue)
    print(item["value"].stringValue)
    print("#############")
    arrayOfMyStruct.append(MyStruct(id: item["id"].intValue, value: item["value"].stringValue))
}

form.last! // recup猫re la derni猫re section
    <<< PushRow<MyStruct>(idItem) {
        $0.title = item["libItem"].stringValue
        $0.options = arrayOfMyStruct
    }

it's work !

But now, there is an another problem (with value selected) ==> #514

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaylyerly picture jaylyerly  路  3Comments

iBearKh picture iBearKh  路  3Comments

pteasima picture pteasima  路  3Comments

calli23 picture calli23  路  3Comments

Tomas1405 picture Tomas1405  路  3Comments