xcode Swift 3:从“UIView?”隐式强制的表达式 去任何
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40386836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3: Expression implicitly coerced from 'UIView?' to Any
提问by RyJ
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.
其他人肯定在将 iOS 项目转换为 Swift 3 时(或之后)收到了此消息,但是当我进行 Google 搜索时,却没有得到相关结果。
Anyway, after converting to Swift 3, I have about 30 warnings that say:
无论如何,在转换为 Swift 3 后,我有大约 30 个警告说:
Expression implicitly coerced from 'UIView?' to Any
从“UIView?”隐式强制的表达式 去任何
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?
但是警告并不指向任何特定的代码行。他们只引用存在警告的类。
有没有人了解这个警告或者我如何让他们沉默?
采纳答案by Madson Cardoso
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]
回答by Michael Peterson
This will happen when the function you are calling has a parameter of type Any, and you are passing an optional.
当您调用的函数具有Any类型的参数并且您传递一个可选的.
For example:
例如:
let color: UIColor? = UIColor.red
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color], for: .normal)
Notice that coloris of type UIColor?and that setTitleTextAttributesexpects a dictionary of type [String: Any]?.
注意颜色是UIColor类型吗?并且setTitleTextAttributes需要类型为[String: Any]的字典?.
In order to avoid the warning you have to either force unwrap your optional, or cast it to Any.
为了避免警告,您必须强制解开您的可选项,或将其强制转换为 Any。
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color!], for: .normal)
or
或者
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: color as Any], for: .normal)
回答by Daniel
Looks like a bug in the Swift compiler:
看起来像是 Swift 编译器中的一个错误:
https://bugs.swift.org/browse/SR-2921
https://bugs.swift.org/browse/SR-2921
Currently, I'm seeing this with Xcode 8.1 and 8.2 beta 1.
目前,我在 Xcode 8.1 和 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.
在您的情况下,警告应该标识源文件,而不是行 - 正如您所说。您将不得不四处寻找对带Any
参数的函数的调用。
Good new is that it appears fixed in an upcoming Swift toolchain.
好消息是它在即将推出的 Swift 工具链中得到修复。
I believe this is fixed in Xcode 8.3 beta 1 (but have not confirmed)
我相信这已在 Xcode 8.3 beta 1 中修复(但尚未确认)
回答by user1046037
Problem
问题
The expected type is Any
but the type provided was UIView?
预期的类型是Any
但提供的类型是UIView?
The problem is with the optional, just make sure an instance of UIView
is passed and things would work.
问题在于optional,只需确保UIView
传递了一个实例并且事情会起作用。