Swift - 使用 where 子句在 for in 循环中解包可选

Swift - Unwrap optional in for in loop with where clause

我有一个 class 和一个可选成员:

class A {
    var i: Int? = nil
}

然后我有一个 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! ...
}

Swift可以吗?

我认为这不可能。

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

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

你可以试试这个

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

  print( i )
}

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

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

   print( i )
}

1.Maybe 你可以使用flatMap获取值i,然后打印它

arr.flatMap{ [=10=].i }.forEach{ print([=10=]) }

2.or 尝试简单的 guard 语句

arr.forEach { element in
    guard let i = element.i else { return }
    print(i)
}

您可以使用 case let syntax,但不能没有 map 的帮助,结果不是最可读的:

for case let .some(unwrapped) in arr.map(\.i) {
    print(unwrapped)
}

如果您是投射外部对象,例如:

for case let object as String in arrayOfAny {
    if object.hasPrefix("tw") {
        print("Starts with 'tw'")
    }
}

而不是:

for object in arrayOfAny where object is String {
    if object.hasPrefix("tw") { // Error: Value of type 'Any' has no member 'hasPrefix'
        print("Starts with 'tw'")
    }
}