swift Logical Operators orderly
It's called Short-circuit Evaluation and it means when a statement stops evaluation as soon as an outcome is determined. The parts of an expression containing &&
or ||
operators are evaluated only until it’s known whether the condition is true or false. This speeds up the execution of expression evaluation.
So in your code true || addOne() && addOne() && addOne()
is known to be true
as soon as you find true
in your expression. In general true || whatever
will always be true, so there is no need to evaluate whatever
.
&&
has precedence over ||
so :
true || addOne() && addOne() && addOne()
is equivalent to :
true || (addOne() && addOne() && addOne())
And we already know that true
OR whatever
is true, so whatever
won't be evaluated.
To change the default precedence in your expression use parentheses where you see fit. For example:
(true || addOne()) && (addOne() && addOne())
In this case, the first addOne()
won't be evaluated, But the second and third will be, since true
AND something
is something
. And thus, in this case, x would be equal to 2 (supposing that initially x = 0).
Here is a final example (I suppose you get it by now, if not let me know in the comments):
if (true || addOne() && addOne()) && addOne()
In this case, there is an OR between true
and addOne() && addOne()
. Without evaluating addOne() && addOne()
we already know that true || addOne() && addOne()
is true
. So the expression could be simplified to true && addOne()
. Here we have to evaluate addOne()
, which means, x
will equal 1
.
Edit
Swift operators belong to Precedence Groups (or levels) which are used to decide which operation has more priority in the evaluation of an expression. A higher precedence level means more priority.
The logical AND &&
belongs to the LogicalConjunctionPrecedence group, which has a higher precedence than the LogicalDisjunctionPrecedence group, to which the logical OR ||
belongs to. And thus &&
has more precedence than ||
.
To learn more about operator precedence groups/levels, have a look right at the bottom of this, or that.
Operator precedence overloading in Swift
Hmm. It actually appears that you can.
operator infix + { associativity left precedence 140 }
operator infix * { associativity left precedence 30 }
let x = 3 + 4 * 5 // outputs 35
But as far as I can tell, this can only be done at "file scope", at least according to a compiler error produced by including this within a class.
'operator' may only be declared at file scope.
odd numbers for operator precedence levels
According to Apple's Operation Declaration documentation
The precedence level can be any whole number (decimal integer) from 0
to 255Although the precedence level is a specific number, it is significant
only relative to another operator.
The simple answer is that 90 to 160 fall near the center of the 0 to 255 range.
Now if you check all of Apple's binary expressions documentation, you will see that the default operators range from a precedence of 90 to a precedence of 160, as you stated in your question. This is a range of 70 and because precedence values are relative to each other, any starting/ending point for this range could be chosen.
However, if they made the default values 0 to 70 or 185 to 255 then when a user created a custom operator, they could not give it a lower precedence than 0 or a higher precedence than 255, causing the operator to be equal to the precedence of Assignment operators or Exponentiative operators respectively.
Therefore, the only logical thing to do is to start this range in the middle of the 0 to 255 range and rather than set the default values of the range to 93 - 163 (the closest to the actual center of the range as possible), they chose to choose the multiples of 10 (90 to 160) instead because in actuality the values do not matter except in relation to each other.
Custom Operators in Swift
I don't know how swift does it, but it's not that difficult.
One approach is to first construct an AST in which an expressions is just a list of operands (including parenthesized subexpressions) and operators. Once the initial parse is complete and operators are declared, precedence (and fixity, if necessary) can be attached to each operator, and the AST is rescanned, rebuilding each expression list as an expression tree using a simplified form of the shunting-yard algorithm.
In the case of Swift, it appears that white-space is used to distinguish between prefix, postfix and infix operators (shades of Fortress!). It's not clear to me whether a sequence of operators would need to be rescanned once all operator names are known, but I can't see any other way of doing it since the whitespace rules do not allow a prefix operator (for example) to be followed by a space in order to separate it from a following prefix operator. That doesn't complicate the above algorithm much, since operators, operands and parentheses do not share any common characters. So even if it is not clear where every operator token starts, the worst that can happen is that you end up having to split an operator token into several actual operators.
Why shift right operator in swift and java get diffrence result
Java is calculating
d >> (1+1)
which is 6.
Swift is calculating
(d >> 1) + 1
which is 13.
You can use parentheses to specify which calculation you want to do.
This is because of operator precedence:
- In Swift -
https://developer.apple.com/documentation/swift/operator_declarations -<<
comes before+
. - In Java - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html -
+
comes before<<
Adjacent operators are in non-associative precedence group 'ComparisonPrecedence' error in Swift
You need ||
, not |
. ||
is "logical or". |
is "bitwise or".
And when you mix ||
and &&
, you need parentheses to avoid any ambiguity.
Based on your description, you want:
if appPurchased == false &&
enabled == true &&
(button == photoLibraryBtn ||
button == takeVideoBtn) {
continue
}
This can also be written as:
if !appPurchased &&
enabled &&
(button == photoLibraryBtn ||
button == takeVideoBtn) {
continue
}
Related Topics
Updating Uitableview with Multiple Sections from Rlmresults.Observe()
Attaching Audiounit Effects to Scnaudiosource Nodes in Scenekit
Skip Item When Performing Map in Swift
Swift 3, Xcode 8 Instantiate View Controller Is Not Working
Convert Emoji to Hex Value Using Swift
Swift Uipasteboard Not Copying Png
How to Add Constraints Programmatically to My Uilabel
Adding a View to the Window Hierarchy
Swift Protocol Generic as Function Return Type
Differences Between "Static Var" and "Var" in Swift
Getting Path for Resource in Command Line Tool
Realm Mobile Platform, How to Connect While Offline
What Is an Example of Drawing Custom Nodes with Vertices in Swift Scenekit
Resize a Sklabelnode Font Size to Fit
Swift Convert Currency String to Double
How to Represent Magnitude for Mass in Swift
With Firebase, Swift Removeobserver(Withhandle Does Not Remove the Observer