0

我正在使用 Eureka 表单构建器构建一个表单,但不明白如何获取表单中的值。他们在此处的文档中提供说明

表单结果被传递给字典:

您可能已经注意到结果字典键是行标记值,值是行值。只有带有标签值的行才会被添加到字典中。

我的代码:

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"])

}

这里是制作 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
    }
}
4

1 回答 1

6

我自己想通了。我并不清楚您需要在要从中检索值的行上设置标签:

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

let dict = form.values(includeHidden: true)

print(dict["NameRow"]) // Prints my name
于 2016-06-09T04:37:50.023 回答