63

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
Grimxn
  • 22,115
  • 10
  • 72
  • 85
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75

3 Answers3

139

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.

Grimxn
  • 22,115
  • 10
  • 72
  • 85
  • 7
    Yet another change. Everywhere in the documentation the where clause is still present. Anyway using the form you suggested works fine. – Fabrizio Bartolomucci Aug 04 '16 at 08:54
  • Is this change described anywhere else than in the (not fully public) Xcode 8 Beta 4 release notes? E.g. evolution/other official docs? I might note to other readers that we're still prompted to use the `where` clause to separate the variable binding from the conditional if using e.g. Swift 3.0-dev in the IBM Sandbox. – dfrib Aug 04 '16 at 08:56
  • The documentation updated to Swift 3 still has where as well the online one. To say this is confusing is an understatement. It now seems they are changing things just for the sake of it, like for the DispatchQueue that now needs deadline instead of when. – Fabrizio Bartolomucci Aug 04 '16 at 08:59
  • @FabrizioBartolomucci Swift 3.0 is still in beta so this is expected. I do believe, however, that all somewhat noteworthy changes should be included in [Swift evolution](https://github.com/apple/swift-evolution), but I haven't seen this one covered there (maybe I've simply missed it). – dfrib Aug 04 '16 at 09:01
  • In fact the just updated Swift documentation now has the comma at page 61 instead of where. – Fabrizio Bartolomucci Aug 04 '16 at 09:02
  • 3
    @dfri It was a part of [SE-0099 "Restructuring Condition Clauses"](https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md) – Hamish Aug 04 '16 at 09:03
  • 1
    @Grimxn Well, it's your answer ;) I generally don't like to edit other people's answers without their permission, unless it's grammar related – although I have edited the link in at your request. – Hamish Aug 04 '16 at 09:09
  • This syntax is so bloody inconsistent it makes me want to scream. It's like we have different people with wildly different philosophies developing different parts of the language and are left with this mess that is Swift 3. RIP consistent syntax. :( – devios1 Feb 13 '17 at 21:00
10

Example with two conditions

if let x = y, let a = b, a == x && !x.isEmpty {
Alex
  • 1,603
  • 1
  • 16
  • 11
4

In xcode 9

if let str = textField.text as String!, !str.isEmpty
{
   params[key] = str
   TextFieldHelper.setup(textField: textField)
}
else
{ 
   TextFieldHelper.error(textField: textField)
}
luhuiya
  • 2,129
  • 21
  • 20
  • To clarify: it is the version of the Swift language—not the version of Xcode—which determines the available syntax. – Jacob Jul 23 '20 at 23:35