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

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.redUIBarButtonItem.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)


Looks like a bug in the Swift compiler:

https://bugs.swift.org/browse/SR-2921

Currently, I'm seeing this with Xcode 8.1 and 8.2 beta 1.

In your case, the warning should identify the source file, but not the line - as you stated. You will have to hunt around for calls to functions with Any parameters.

Good new is that it appears fixed in an upcoming Swift toolchain.

I believe this is fixed in Xcode 8.3 beta 1 (but have not confirmed)