Shyam Shyam - 5 months ago 130
iOS Question

How to extract values from Eureka forms in swift to local variables?

While I try to pass it to temporary variables it doesn't seem to happen.

Although there are no errors while building the app, once I try to enter a value for "rateOfPaddy", it fails, citing "fatal error: unexpectedly found nil while unwrapping an Optional value"

Please let me know if I'm doing anything wrong, either related to Swift or Eureka?

form +++ Section()
<<< DateTimeRow() {
$0.tag = "RecordDateTag"
$0.title = "Date"
$0.value = NSDate()
}.cellSetup { cell, row in
cell.textLabel?.textColor = UIColor.blackColor()
}.onCellHighlight { _ in
if let dateInput = formInput["RecordDateTag"] as? String {
self.dateInputValue = dateInput
}
}.onCellUnHighlight { _ in
if let dateInput = formInput["RecordDateTag"] as? String {
self.dateInputValue = dateInput
}
}


I used .onChange callback to check and pass the information to local variables, which was of no use. .onCellHighlight and .onCellUnHighlight combination didn't do the trick either!!

Answer

Try calling values function as documented here

You can create a method like below and call it from onChange callbacks

func updateValues() {
    let allFormData = formInput.values()

    if let dateInput = allFormData["RecordDateTag"] as? String {
        self.dateInputValue = dateInput
    }
}