Fabrizio Bartolomucci Fabrizio Bartolomucci - 4 months ago 10
iOS Question

Usage of where in if let assignment in Swift

The Swift documentation at page 61 of the Swift manual hints to the possibility of using where to join an optional binding with a regular condition with where. Yet when I do it I have a waning suggesting me to substitute the where with a comma like in the following piece of code:

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

Answer

In Swift 3 this syntax has changed.

What was

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

Is now

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

The justification is that each sub-clause of the if ... { is now an independent boolean test.

See the Xcode Release notes & the Swift Evolution proposal for more info about this change.