Usage of where in if let assignment in Swift

30,395

Solution 1

Example with two conditions

if let x = y, let a = b, a == x && !x.isEmpty {

Solution 2

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.

Solution 3

In xcode 9

if let str = textField.text as String!, !str.isEmpty
{
   params[key] = str
   TextFieldHelper.setup(textField: textField)
}
else
{ 
   TextFieldHelper.error(textField: textField)
}
Share:
30,395
Fabrizio Bartolomucci
Author by

Fabrizio Bartolomucci

Graduated in Computer Science long ago I got interested in iOS programming during a leeway for my ordinary job and have developed 6 apps to the present all of them on sale on the AppStore. I am proficient in many programming languages including Objective-C, c++, php, c#, delphi and sql.

Updated on July 08, 2022

Comments

  • Fabrizio Bartolomucci
    Fabrizio Bartolomucci almost 2 years

    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. Yet when I do it I have a warning 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