Usage of where in if let assignment in Swift

前端 未结 3 1350
囚心锁ツ
囚心锁ツ 2021-01-31 06:56

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 i

相关标签:
3条回答
  • 2021-01-31 07:51

    Example with two conditions

    if let x = y, let a = b, a == x && !x.isEmpty {
    
    0 讨论(0)
  • 2021-01-31 07:56

    In xcode 9

    if let str = textField.text as String!, !str.isEmpty
    {
       params[key] = str
       TextFieldHelper.setup(textField: textField)
    }
    else
    { 
       TextFieldHelper.error(textField: textField)
    }
    
    0 讨论(0)
  • 2021-01-31 08:00

    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.

    0 讨论(0)
提交回复
热议问题