Swift:如何使用Eureka表单构建器获取表单值?

时间:2016-06-09 04:03:32

标签: swift eureka-forms

我正在使用Eureka表单构建器构建表单,但不了解如何在表单中获取值。 They provide instructions in the docs here

表单结果传递给字典:

  

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

我的代码:

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

}

这里是制作词典的公共功能

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 个答案:

答案 0 :(得分:6)

我自己想通了。我不是很清楚你需要在要从中检索值的行上设置标记:

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

let dict = form.values(includeHidden: true)

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