Preface
As we all know, in Swift language, Bool values are used for conditional judgments, and operations of && and || can be performed to achieve joint judgment of multiple expressions.
However, due to the fact that there is optional binding in Swift, or the use of let to expand optional options, some places cannot be used to process it in the condition judgment. For example, when the variable hasValue does have a value and the number of parameters paramCount is greater than 0, we can execute the code. Generally, we can write it like this:
if hasValue != nil && paramCount > 0 { ... }
However, if we want to use the value of hasValue in the subsequent code, we cannot just judge whether hasValue is nil, but should use optional binding to read the value, which is the following code:
if let hasValue = hasValue { if paramCount > 0 { ... } }
becauselet hasValue = hasValue
It will not return a Bool value, resulting in the two conditions being unable to be judged using &&. At this time, we have to use the so-called comma, which means we can write it as:
if let hasValue = hasValue, paramCount > 0 { ... }
This way, we can meet our needs. The code forces the above code to lack a layer of judgment, which looks more friendly. Especially when we need options to bind multiple variables, writing will be more convenient. like:
if let a = a, let b = b, let c = c, let d = d, e < 0, f > 0 { ... }
If you do not use it and divide it, but make one judgment after another, it will enter the pit of multiple judgments, making the code bloated.
In general, the function of commas in conditional judgment is similar to &&, but in addition to connecting Bool values, it can also be used to connect the judgment of optional bindings.
Summarize
The above is the entire content of this article. There are still many shortcomings in this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.