Reputation: 2853
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
}
}
Upvotes: 0
Views: 2907
Reputation: 6775
Just another syntax to add tag
would be to pass it within parenthesis:
<<< NameRow("NameRow") { // Notice the tag being passed as String
$0.title = "Name:"
$0.value = "My name"
}
let dict = form.values(includeHidden: true)
print(dict["NameRow"]) // Will Still Print the name
Reference taken from this Stack Overflow answer
Upvotes: 0
Reputation: 2853
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
Upvotes: 6