Swift: How to get form values ​​using Eureka form builder?

I am creating a form with Eureka form creator, but do not understand how I can get the values ​​inside the form. They provide instructions in the docs here .

The results of the form are passed to the dictionary:

As you may have noticed, the key of the result word is the value of the string tag and the value is the value of the string. Only strings with tag value will be added to the dictionary.

My code:

override func viewDidLoad() {
    super.viewDidLoad()

    form =

        Section()

        <<< NameRow() { // NameRow is dictionary key, right?
            $0.title = "Name:"
            $0.value = "My name" // This is what should be printed
        }

        let dict = form.values(includeHidden: true)

        // PROBLEM: This prints nil
        print(dict["NameRow"])

}

      

And here's the public function that makes the dict

public func values(includeHidden includeHidden: Bool = false) -> [String: Any?]{
    if includeHidden {
        return allRows.filter({ $0.tag != nil })
            .reduce([String: Any?]()) {
                var result = $0
                result[$1.tag!] = $1.baseValue
                return result
        }
    }
    return rows.filter({ $0.tag != nil })
        .reduce([String: Any?]()) {
            var result = $0
            result[$1.tag!] = $1.baseValue
            return result
    }
}

      

0


source to share


1 answer


I figured it out myself. It didn't immediately become clear to me that you need to set tags on the lines you want to get the value from:



<<< NameRow() {
    $0.tag = "NameRow"
    $0.title = "Name:"
    $0.value = "My name"
}

let dict = form.values(includeHidden: true)

print(dict["NameRow"]) // Prints my name

      

+6


source







All Articles