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

时间:2022-09-11 18:39:08

I have a class with an optional member :

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

class A {
    var i: Int? = nil
}

Then I have an array of objects of type A. Some objects in the array have a value for i, some others don't.

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

I want to iterate over objects in the array that have a value for i while unwrapping the optional at the same time. I didn't find a way to do both at the same time (I don't even know if it's possible), forcing me to write a if let construct inside the loop.
For example :

我想迭代数组中具有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! ...
}

Is it possible in Swift ?

在Swift中有可能吗?

2 个解决方案

#1


2  

I don't think that's possible.

我不认为这是可能的。

Even if you have a where clause in your loop the type of obj is still of type A and as such i still remains optional.

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

To see why this is so think about the fact that you can change the value of i on object obj inside the loop, so the compiler is not sure that the value of i is valid until you unwrapp it.

要知道为什么会这样想想你可以在循环中改变对象obj的i值,所以编译器不确定i的值是否有效直到你解开它。

You can try something like this

你可以尝试这样的事情

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

  print( i )
}

but if you start using guard you also skip the where clause

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

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

   print( i )
}

#2


7  

1.Maybe you can use flatMap to get value i, then print it

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

arr.flatMap{ $0.i }.forEach{ print($0) }

2.or Trying simple guard statement

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

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

#1


2  

I don't think that's possible.

我不认为这是可能的。

Even if you have a where clause in your loop the type of obj is still of type A and as such i still remains optional.

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

To see why this is so think about the fact that you can change the value of i on object obj inside the loop, so the compiler is not sure that the value of i is valid until you unwrapp it.

要知道为什么会这样想想你可以在循环中改变对象obj的i值,所以编译器不确定i的值是否有效直到你解开它。

You can try something like this

你可以尝试这样的事情

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

  print( i )
}

but if you start using guard you also skip the where clause

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

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

   print( i )
}

#2


7  

1.Maybe you can use flatMap to get value i, then print it

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

arr.flatMap{ $0.i }.forEach{ print($0) }

2.or Trying simple guard statement

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

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