Search code examples
swiftdictionaryoption-typeeureka-forms

Eureka Get Dictionary Values Swift


I am using the Eureka form library found here. I am trying to iterate over the dictionary values of the form and only print the values that are true; not nil or not false. So far I have

     let valuesDictionary = form.values()

        for (_, version) in valuesDictionary
        {

            if version != nil || version as! Bool != false // error here

            {
                print (version!)
            }

        }

I am getting the following error on the if statement:

fatal error: unexpectedly found nil while unwrapping an Optional value


Solution

  • Do it like this:

    for (_, version) in valuesDictionary
    {
        if let version = version, version as? Bool != false {
            print(version)
        } 
    }
    

    This will translated as:

    • if let version = version: if there is a value (version is not nil)

    After checking if it's not null (optional binding), as a where condition:

    • version as? Bool != false: checks tow points:

    1- is version could be cast to Bool.

    2- If the first point is true, check if version is not false.