Swift: How to get form values using Eureka form bu

2019-01-27 11:43发布

I'm building a form with the Eureka form builder but don't understand how I can get the values inside the form. They provide instructions in the docs here.

The form results are passed to a dictionary:

As you may have noticed the result dictionary key is the row tag value and the value is the row value. Only rows with a 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 the public func 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
    }
}

1条回答
对你真心纯属浪费
2楼-- · 2019-01-27 12:04

I figured it out myself. It wasn't immediately clear to me that you need to set tags on the rows you want to retrieve 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
查看更多
登录 后发表回答