Swift 3: Expression implicitly coerced from 'UIView?' to Any

In my case it was an issue related to a dictionary without explicit type:

let dict = ["key": value]

Than I solved specifying the type:

let dict: [String: Any] = ["key": value]

In your case you can specify your value type:

let dict: [String: UIView] = ["key": value]

This will happen when the function you are calling has a parameter of type Any, and you are passing an optional.

For example:

let color: UIColor? = UIColor.red
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color], for: .normal)

Notice that color is of type UIColor? and that setTitleTextAttributes expects a dictionary of type [String: Any]?.

In order to avoid the warning you have to either force unwrap your optional, or cast it to Any.

UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color!], for: .normal)

or

UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color as Any], for: .normal)

Tags:

Ios

Xcode

Swift3