1

Trying to return a JSON output using Swift's Codable library. I've set up a struct to capture a 'default' value from my JSON. The 'default' value currently has two datatype outputs, string or int. I'm trying to return the string value if the 'default' is string and the int value if it's is an int.

struct Settings: Codable {
   let configDefault: IntOrString
   let name: String?
}

enum IntOrString: Codable {
        case integer(Int)
        case string(String)

        init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            if let int = try? container.decode(Int.self) {
                self = .integer(int)
                return
            }
            if let string = try? container.decode(String.self) {
                self = .string(string)
                return
            }
            throw DecodingError.typeMismatch(IntOrString.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Default"))
        }

        func encode(to encoder: Encoder) throws {
            var container = encoder.singleValueContainer()
            switch self {
            case .integer(let x):
                try container.encode(x)
            case .string(let x):
                try container.encode(x)
            }
        }
    }

All this gets decoded into a value defaultValueHolder

let defaultValueHolder = try JSONDecoder().decode(Settings.self, from: jsonString)

Using Eureka's form library i'm trying to set the table value as the default String or Int returned from the JSON.

form +++ Section ("Values")
    // dynamically generates rows
    for values in defaultValueHolder {
            form.last! <<< IntRow() {
                $0.tag = values.name
                $0.title = values.name
                $0.value = values.IntOrString
    }

The issue is when trying to add a value to $0.value I cannot cast it as a IntORString enum type and have to return either the int or string depending if a IntRow or TextRow is declared.

1 Answer 1

0

Actually IntRow and TextRow rows will accept only the type that has been defined for their cells as below,

open class IntCell: _FieldCell<Int>, CellType 
open class TextCell: _FieldCell<String>, CellType

this is the reason why you have to extract the value from your IntOrString enumeration type to assign to the respective cell's row .


If you wish to have a row that accepts your type(IntOrString) then you have to conform your new type to Equatable as eureka cell's accept value types that conform to Equatable. Something as below,

enum IntOrString: Codable, Equatable

Then you should add a new cell with create xib file as marked to create your custom view shown below,

enter image description here

And then you can update the IntOrStringTableViewCell as below,

import UIKit
import Eureka

class IntOrStringTableViewCell: Cell<IntOrString>, CellType {

    @IBOutlet private weak var valueLabel: UILabel!

    override func setup() {
        super.setup()
        self.updateValue()
    }

    override func update() {
        super.update()
        self.updateValue()
    }

    private func updateValue() {
        guard let value = self.row.value else { return }
        if case .integer(let int) = value {
            self.valueLabel.text = "\(int)"
        } else if case .string(let string) = value {
            self.valueLabel.text = string
        }
    }
}

final class IntOrStringRow: Row<IntOrStringTableViewCell>, RowType {

    required init(tag: String?) {
        super.init(tag: tag)

        self.cellProvider = CellProvider<IntOrStringTableViewCell>(nibName: "IntOrStringTableViewCell", bundle: Bundle.main)
    }
}

Now when you will use this row, you will be able to assign the IntOrString value as it is without any casting

form +++ Section ("Values")
    // dynamically generates rows
    for values in defaultValueHolder {
         form.last! <<< IntOrStringRow() {
            $0.tag = values.name
            $0.title = values.name
            $0.value = values.IntOrString
}
9
  • Thanks @Kamran for your detailed reply, mostly seems to be working. Except i'm getting a print out of "integer(2)" or "string("test") in the $0.value = values.IntOrString output. Is there a way to just return the string or int value. Commented Jul 1, 2018 at 23:18
  • are you able to help with this issue I have above? Commented Jul 2, 2018 at 2:34
  • In cell setup you should display the exact value extracted from enum type
    – Kamran
    Commented Jul 2, 2018 at 2:41
  • I've started adding "row.value = ..." under the super.setup() in the cell set up but aren't sure what to be setting there. Is this what you meant about the cell set up? Commented Jul 2, 2018 at 3:13
  • Yes, connect your label IBOutlet in cell and set text value from row value
    – Kamran
    Commented Jul 2, 2018 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.