xcode Swift 3 警告:在检查选项时使用了“字符串”类型的非可选表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40884167/
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 warning: Non-optional expression of type 'String' used in a check for optionals
提问by nontomatic
I'm updating a project to Swift 3 and came across the following warning which I can't seem to resolve.
我正在将一个项目更新到 Swift 3 并遇到以下我似乎无法解决的警告。
fileprivate var filteredTitlesList: [String] = []
if let filteredTitle: String = filteredTitlesList[indexPath.row] as String { // 'Non-optional expression of type 'String' used in a check for optionals'
// Do something
}
The answer to a similar question here didn't help me: Non-optional expression of type 'AnyObject' used in a check for optionals
此处对类似问题的回答对我没有帮助:检查选项中使用的“AnyObject”类型的非可选表达式
Thanks a lot!
非常感谢!
回答by Jacob King
You are trying to unwrap a value that is already unwrapped, and hence you are getting an error because it doesn't need unwrapping again. Change your if statement to look like the following and you should be golden:
您正在尝试解开一个已经解包的值,因此您收到一个错误,因为它不需要再次解包。将您的 if 语句更改为如下所示,您应该是金色的:
if filteredTitleList.count > indexPath.row {
let filteredTitle = filterdTitleList[indexPath.row]
}
Unfortunately there is no way to bind the variable within the if statement, hopefully they'll add one in the future.
不幸的是,没有办法在 if 语句中绑定变量,希望他们将来会添加一个。
回答by Ali
Other possibility of this warning is when you're trying to put a statement eg:let keyboardFrame: CGRect = keyboardFrameValue.cgRectValue
in conditional statement like if
or guard
此警告的其他可能性是当您尝试放置语句时,例如:let keyboardFrame: CGRect = keyboardFrameValue.cgRectValue
在条件语句中,例如if
或guard