Swift 3 警告:检查选项时使用的“字符串"类型的非可选表达式 [英] Swift 3 warning: Non-optional expression of type 'String' used in a check for optionals
问题描述
我正在将一个项目更新到 Swift 3,但遇到了以下我似乎无法解决的警告.
I'm updating a project to Swift 3 and came across the following warning which I can't seem to resolve.
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
}
这里对类似问题的回答对我没有帮助:用于检查选项的AnyObject"类型的非可选表达式
The answer to a similar question here didn't help me: Non-optional expression of type 'AnyObject' used in a check for optionals
非常感谢!
推荐答案
您正在尝试解包一个已经解包的值,因此您收到错误,因为它不需要再次解包.将您的 if 语句更改为如下所示,您应该是金色的:
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 filteredTitleList.count > indexPath.row {
let filteredTitle = filterdTitleList[indexPath.row]
}
遗憾的是没有办法在 if 语句中绑定变量,希望他们将来会添加一个.
Unfortunately there is no way to bind the variable within the if statement, hopefully they'll add one in the future.
这篇关于Swift 3 警告:检查选项时使用的“字符串"类型的非可选表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!