一尘不染

Swift:如何使用Eureka表单生成器获取表单值?

swift

我正在使用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"])

}

在这里,制定命令的公共职能

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
    }
}

阅读 345

收藏
2020-07-07

共1个答案

一尘不染

我自己弄清楚了。现在还不清楚我是否需​​要在要从中检索值的行上设置标签:

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

let dict = form.values(includeHidden: true)

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