Swift - 使用where子句展开for循环中的可选项

Vin*_*nce 7 optional for-in-loop swift

我有一个带有可选成员的类:

class A {
    var i: Int? = nil
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个类型的对象数组A.数组中的某些对象具有值i,而其他对象则没有.

我想迭代数组中具有值的对象,i同时解包可选项.我没有找到同时做两件事的方法(我甚至不知道是否可能),迫使我if let在循环中编写一个构造.
例如 :

// a1, a2 have a value for i
let arr: [A] = [a1, a2, a3]
for obj in arr where obj.i != nil {
    // I want to avoid if let, or force unwrapping here
    if let unwrapped = obj.i {
        print(i)
    }
    // let unwrapped = obj.i! ...
}
Run Code Online (Sandbox Code Playgroud)

在Swift中有可能吗?

Sai*_* Li 7

1.也许你可以使用flatMap获取值i,然后打印出来

arr.flatMap{ $0.i }.forEach{ print($0) }
Run Code Online (Sandbox Code Playgroud)

2.或尝试简单的警卫声明

arr.forEach { element in
    guard let i = element.i else { return }
    print(i)
}
Run Code Online (Sandbox Code Playgroud)


Rad*_*iță 3

我认为这是不可能的。

即使循环中有一个where子句, obj的类型仍然是A类型,因此i仍然是可选的。

要了解为什么会这样,请考虑以下事实:您可以在循环内更改对象obj上的i值,因此编译器不确定i的值是否有效,直到您解开它。

你可以尝试这样的事情

for obj in arr where obj.i != nil {
  guard let i = obj.i else { continue }

  print( i )
}
Run Code Online (Sandbox Code Playgroud)

但如果你开始使用guard,你也会跳过where子句

for obj in arr {
   guard let i = obj.i else { continue }

   print( i )
}
Run Code Online (Sandbox Code Playgroud)