Using the Swift if let with logical AND operator &&

前端 未结 6 457
名媛妹妹
名媛妹妹 2021-01-30 19:13

We know that we can use an if let statement as a shorthand to check for an optional nil then unwrap.

However, I want to combine that with another expression

6条回答
  •  有刺的猬
    2021-01-30 20:08

    Max's answer is correct and one way of doing this. Notice though that when written this way:

    if let a = someOptional where someBool { }

    The someOptional expression will be resolved first. If it fails then the someBool expression will not be evaluated (short-circuit evaluation, as you'd expect).

    If you want to write this the other way around it can be done like so:

    if someBool, let a = someOptional { }

    In this case someBool is evaluated first, and only if it evaluates to true is the someOptional expression evaluated.

提交回复
热议问题