问题描述
此
NSAttributedString.Key.foregroundColor: view.tintColor
触发此警告
Expression implicitly coerced from 'UIColor?' to 'Any'
但警告不应该
Expression implicitly coerced from 'UIColor?' to 'UIColor'
由于此属性
NSAttributedString.Key.foregroundColor
是UIColor
类型吗?
更多内容:
override func viewDidLoad() {
super.viewDidLoad()
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 40),
NSAttributedString.Key.foregroundColor: view.tintColor
], for: .normal)
}
推荐答案
这与.foregroundColor
无关.它与.tintColor
和setTitleTextAttributes
有关.
This has nothing to do with .foregroundColor
. It has everything to do with .tintColor
and setTitleTextAttributes
.
此参数的类型为[NSAttributedString.Key : Any]
.绝不考虑每个密钥的文档.它不知道也不在乎这应该是UIColor
.如果您通过了"squid",它将在没有警告的情况下进行编译(虽然不起作用,但会编译):
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)
所要查看的是您正在将view.tintColor
分配给类型为Any
的值.
All it's looking at is that you're assigning view.tintColor
to a value of type Any
.
问题是view.tintColor
不是UIColor
,它是UIColor!
. .tintColor
实际上不可能为nil,但是可以将其设置为nil:
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
在ObjC中有意义,但是在Swift中表达它的唯一方法是使用!
类型.当您将!
类型分配给其他事物时,它们将成为?
类型.这意味着您在接受Any
(字典值)的地方使用UIColor?
.
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).
使用可选的Any
可能很危险,因为它会产生很多奇怪的极端情况.例如,您不能通过Any来回传递可选内容;它被压缩为基本类型:
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
与Any一起使用时,会有很多这类小尖锐的边缘.
There are a lot of these kinds of little sharp-edges when working with Any.
当然,您不是平均值与Any一起工作.这不是你的错.但这就是Swift抱怨的原因.
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!
您可以添加as Any
来使警告静音:
You can add as Any
to silence the warning:
.foregroundColor: view.tintColor as Any
我个人会使用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)
这篇关于“强制转换为Any",但属性的类型为UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!