简体   繁体   中英

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

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

The error "fatal error: unexpectedly found nil while unwrapping an Optional value" means that you are trying to unwrap an optional that has nil as a value.

Verify that every formInput[KEY] has a value of the type you expect before forcing the unwrap with the as!

You could benefit from the Optional Binding

if let value = formInput["Some"] as? Int
{
    //Value exist and is an Int
}
else
{
    print("Not an Int")
}

For More references:

Swift Type Casting

Swift Optionals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM