Swift 3: Expression Implicitly Coerced from 'Uiview' to Any

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]

warning: expression implicitly coerced from 'String?' to Any

This warning occurs when you print an optional. The compiler suggests three options to silence the warning. Use the most appropriate one. The warning is harmless.

Expression implicitly coerced from 'UIView?' to Any - How to find line in source code

I managed to clean them all up by process of elimination. I commented out the code a block at a time until a warning disappeared, then inspected that block. It turns out that the culprits were dictionary declarations with mixed data types. I added as [String : Any] to the end of each, and it compiles clean now.

For example:

let params = [
"myInt" : 50,
"myString" : "some stuff",
"myBool" : true
]

needs to be changed to:

let params = [
"myInt" : 50,
"myString" : "some stuff",
"myBool" : true
] as [String : Any]

coerced to Any' but property is of type UIColor

This has nothing to do with .foregroundColor. It has everything to do with .tintColor and setTitleTextAttributes.

This parameter is of type [NSAttributedString.Key : Any]. It is not in any way considering the documentation for each key. It doesn't know or care that this should be a UIColor. If you passed "squid", this would compile without warning (it wouldn't work, but it would compile):

UIBarButtonItem.appearance().setTitleTextAttributes(
[
.font: UIFont.systemFont(ofSize: 40),
.foregroundColor: "squid",
], for: .normal)

All it's looking at is that you're assigning view.tintColor to a value of type Any.

The problem is that view.tintColor is not UIColor, it's UIColor!. It's not actually possible for .tintColor to be nil, but it's possible to set it to nil:

view.tintColor        // r 0.0 g 0.478 b 1.0 a 1.0
view.tintColor = .red
view.tintColor // r 1.0 g 0.0 b 0.0 a 1.0
view.tintColor = nil
view.tintColor // r 0.0 g 0.478 b 1.0 a 1.0

That makes sense in ObjC, but the only way to express it in Swift is to use a ! type. When you assign ! types to other things, they become ? types. And that means that you're using UIColor? in a place that accepts Any (the value of the dictionary).

Using an optional as Any can be dangerous because it creates a lot of weird corner cases. For example, you can't round-trip an optional through Any; it gets squashed into its base type:

let x: Any = Optional(1)
x as? Int? // Cannot downcast from 'Any' to a more optional type 'Int?'
x as? Int // 1

There are a lot of these kinds of little sharp-edges when working with Any.

Of course you didn't mean to work with Any. It's not your fault. But this is why Swift is complaining.

There are several solutions, depending on what you like. You can use a !:

    .foregroundColor: view.tintColor!

You can add as Any to silence the warning:

    .foregroundColor: view.tintColor as Any

Personally I'd use as Any.

Or you can be elaborate and unload the value earlier (I don't recommend this):

let tintColor = view.tintColor ?? .blue

UIBarButtonItem.appearance().setTitleTextAttributes(
[
.font: UIFont.systemFont(ofSize: 40),
.foregroundColor: tintColor,
], for: .normal)

Adding optional value to [Any], got compiler warning - expression implicitly coerced from Double? to Any

It's only a warning. The compiler wants to make sure you know you're doing an odd thing, wrapping an Optional inside an Any.

In the line that's giving trouble, write optionalDouble as Any to suppress the warning.

Why is it necessary to coerce from Optional to Any?

The purpose of Optional is to prevent you from accidentally calling methods on or accessing properties of variables which are nil.

You are right that Optional can be assigned to Any. Anything can be assigned to Any. But look what happens now! I have transformed a value that can be nil into a non-optional type Any! If somehow this value is passed to somewhere else and some other programmer (or you in the future) would assume that it has a non-nil value.

Therefore, the compiler warning is there to remind you that you may have forgotten to unwrap your optionals.



Related Topics



Leave a reply



Submit