52

Someone else must have received this message while (or after) converting an iOS project to Swift 3, yet when I do a Google search, I get no relevant results.

Anyway, after converting to Swift 3, I have about 30 warnings that say:

Expression implicitly coerced from 'UIView?' to Any

But the warnings do not point to any specific line of code. They only reference the class where the warning exists.
Does anyone have an insight into this warning or how I might go about silencing them?

eli-k
  • 10,898
  • 11
  • 40
  • 44
RyJ
  • 3,995
  • 6
  • 34
  • 54
  • Its happened to me after I installed Xcode 8.1 (8B62). In my case I was sending a String object to method with Any object type. And in that case the warning make sense – Mike.R Nov 03 '16 at 09:52
  • Try to find where you pass UIView object to Any type. – Mike.R Nov 03 '16 at 14:37
  • 3
    I would really like to know how to get line numbers for these warnings! – Christopher Nov 29 '16 at 16:09
  • 1
    The fact that "warnings do not point to any specific line of code" was a bug and has been fixed. But the warnings themselves will persist. For example, when I `print` an Optional I get this warning. I find this warning very annoying. Any should mean Any. Why _shouldn't_ I pass a `UIView?` where an Any is expected? – matt Jan 27 '17 at 02:11

4 Answers4

35

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]
Felipe Plets
  • 7,230
  • 3
  • 36
  • 60
  • 1
    This is most definitely the cause of the warnings I was getting. For me it was adding explicit types to the views dictionaries we were using for auto layout. `let views: [String: UIView] = ["button": button]` I made that addition to the code weeks ago and didn't realize it had cleared up those warnings. Thanks man! – RyJ Nov 22 '16 at 18:57
  • 1
    `Any` is different from `Optional(Any)`, i.e. `Any?`, so you have to make sure that value is some kind of non-null type. – DawnSong Jan 04 '17 at 08:24
33

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)
Michael Peterson
  • 10,383
  • 3
  • 54
  • 51
  • 2
    That was it for me. I had a dictionary of type [String: Any] and used an assignment of dict = [ "foo" : bar ] where bar was of type String? (optional). Got rid of the warning by replacing that with dict = [ "foo" : bar ?? "" ] -that way if bar is nil, the value gets set to the empty string which was what I wanted in this case. – n13 Nov 29 '16 at 05:50
15

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)

Daniel
  • 8,794
  • 4
  • 48
  • 71
1

Problem

The expected type is Any but the type provided was UIView?

The problem is with the optional, just make sure an instance of UIView is passed and things would work.

user1046037
  • 16,755
  • 12
  • 92
  • 138