where in if let assignment in Swift 的用法

Usage of where in if let assignment in Swift

Swift 手册第 61 页的 Swift 文档提示了使用 where 加入带有常规条件的可选绑定的可能性。然而,当我这样做时,我收到一条警告,建议我将 where 替换为逗号,如下面的代码所示:

if let geocodingError = error as? NSError where geocodingError.code == 2

在 Swift 3 中,此语法已更改。

什么是

if let x = y, a = b where a == x {

现在

if let x = y, let a = b, a == x {

理由是 if ... { 的每个子句现在都是一个独立的布尔测试。

有关此更改的详细信息,请参阅 Xcode Release notes & the Swift Evolution proposal

具有两个条件的示例

if let x = y, let a = b, a == x && !x.isEmpty {

在xcode9

if let str = textField.text as String!, !str.isEmpty
{
   params[key] = str
   TextFieldHelper.setup(textField: textField)
}
else
{ 
   TextFieldHelper.error(textField: textField)
}