Swift - Unwrap optional in for in loop with where clause

11.9k Views Asked by At

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.

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 :

// 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 ?

3

There are 3 best solutions below

3
On BEST ANSWER

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.

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.

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

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

   print( i )
}
2
On

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

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

2.or Trying simple guard statement

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

You can use case let syntax, but not without the help of map, and the result isn't the most readable:

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

It's more useful if you're e.g. casting the outer object, e.g.:

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

instead of:

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'")
    }
}