I have problem with the (&&) logic in swift, I don't understand why the compiler deal with 'and' logic as 'or' logic, I enter one field and he ignore the others
if (self.txtAddressNickname.text!.isEmpty == true) && (self.txtArea.text!.isEmpty ==
true) && (self.txtHouseNo.text.isEmpty == true) && (self.txtAddressType.text!.isEmpty
== true) && (self.txtStreet.text!.isEmpty == true) && (self.txtHouseNo.text!.isEmpty
== true) && (self.txtMobileNo.text!.isEmpty == true){
// alert
}
You want to show an alert if any of the fields is empty, right?
You need 'or', not 'and'.
if txtAddressNickname.text!.isEmpty ||
txtArea.text!.isEmpty ||
txtHouseNo.text.isEmpty ||
txtAddressType.text!.isEmpty ||
txtStreet.text!.isEmpty ||
txtHouseNo.text!.isEmpty ||
txtMobileNo.text!.isEmpty {
// alert
}
I also tidy your code up little bit to read more easly.
&& works like:
If 1 condition on the way give false, compiler won't check the further conditions in the expression.
Similarly, || works like,
If 1 condition on the way give true, compiler won't check the further conditions in the expression.
So, in your expression any of the initial textField might be empty, so it is not checking further.
AND and OR logic is the same in all programming languages.
A and B is true only if A is true and B is true.
A or B is true if A is true or B is true or both of them are true.
Check the images below, note that 0 is false and 1 is true :
Related
I need some help in understanding the following problem.
I have the need for a dynamic filter (trying to not duplicate the queries for different scenarios).
I have already found that I can create a dynamicFilter = Expression<Bool>(value: true) and then adding the clauses to the filter. As described here
Now I have a database field defined as nullable and want to add a filter if a condition is given:
let hi_time = Expression<Int?>("HI_Time")
if condition {
dynamicFilter = dynamicFilter && (hi_time != nil)
}
However I am getting a warning: Comparing non-optional value of type 'Expression<Int?> to 'nil' always returns true and thus that filter is skipped and not added to the query.
Now as I understand the Expression<Int?> is saying, that the column can contain NULL values.
How can I now append this NULL check to the query when condition X is fullfilled? Can I somehow add Plain SQL here? or do I have to make two separate Queries? The problem is, I will have some other NULL Checks to perform that only are needed if a specific condition is fulfilled.
Thanks so much for the help, it's much appreciated.
EDIT:
var dynamicFilter = Expression<Bool>(value: true)
if cond.temperature.currentKind == .celsius {
dynamicFilter = dynamicFilter && (upperTemp_C >= cond.temperature.getCurrentTemperature() && lowerTemp_C <= cond.temperature.getCurrentTemperature())
} else {
dynamicFilter = dynamicFilter && (upperTemp_F >= cond.temperature.getCurrentTemperature() && lowerTemp_F <= cond.temperature.getCurrentTemperature())
}
if condition.rawValue == 1 {
dynamicFilter = dynamicFilter && (hi_time !== nil)
}
do {
let numberOfHOT = try db.scalar(times
.filter((delDate > Date() || delDate == nil))
.filter(dynamicFilter)
...
SOLUTION:
With the help of Lou Franco the error was in the declaration of the dynamic filter. var dynamicFilter = Expression<Bool?>(value: true) did the trick.
Thanks so much!
To check for inequality between an expression and an optional value with SQLite.swift, you need !==, not !=
Here is the relevant code:
https://github.com/stephencelis/SQLite.swift/blob/master/Sources/SQLite/Typed/Operators.swift#L450
You can also see this in the tests:
https://github.com/stephencelis/SQLite.swift/blob/master/Tests/SQLiteTests/Typed/OperatorsTests.swift#L214
When you don't find something in the docs, the tests in this project are a good place to look for answers
I'm a total newbie, so pardon me if this is an annoying question. I am just practicing and am wondering why this code is printing "Almost perfect. Give it another try!" when it should be printing "Well, you got one correct". Any help would be much appreciated
var subjectOne = true
var subjectTwo = false
var subjectThree: Int = 5
func shout(subjectOne: Bool, subjectTwo: Bool, subjectThree: Int){
if subjectOne && subjectTwo && subjectThree >= 25{
print("HIGH SCORE!")
}
else if subjectOne || subjectTwo && subjectThree >= 10{
print("Almost perfect. Give it another try!")
}
else if subjectOne && subjectTwo && subjectThree >= 10{
print("There ya' go!")
}else if !subjectOne && !subjectTwo && subjectThree >= 10{
print("Well, you got a high enough number at least!")
}else if subjectOne || subjectTwo && subjectThree < 10{
print("Well, you got one correct")
}else if !subjectOne && !subjectTwo && subjectThree < 10{
print("You fail at life!")
}else if subjectOne || subjectTwo && subjectThree >= 25{
print("Almost a high score. Keep going, you got this!")
}
}
shout(subjectOne: subjectOne, subjectTwo: subjectTwo, subjectThree: subjectThree)
This is happening because your first else if clause is evaluating to true due to order of operations:
else if subjectOne || subjectTwo && subjectThree >= 10
evaluates from left to right, so your code first checks if subjectOne is true. Because it is indeed true, and an || (or) comes next, your code no longer needs to evaluate the rest of the expression because it already evaluates as true.
You might have meant to write that clause this way instead:
else if (subjectOne || subjectTwo) && subjectThree >= 10
This would evaluate to false because thanks to the parenthesis, your code knows that it must evaluate both sides of the expression (subjectOne or subjectTwo) AND subjectThree must be greater than or equal to ten.
In other languages, I've done logical expressions like this one without any issues, but I'm having a tough time in Swift.
I want this to evaluate as true if appPurchased = false AND enabled = true AND the button equals either photoLibraryBtn or takeVideoBtn:
for button in buttonList {
if appPurchased == false &&
enabled == true &&
button == photoLibraryBtn |
button == takeVideoBtn {
continue
}
button.isEnabled = enabled
button.isUserInteractionEnabled = enabled
button.alpha = alpha
}
I get the error "Adjacent operators are in non-associative precedence group 'ComparisonPrecedence'", which I can find no results on Google. I also don't see examples such as mine in Swift, so I'm thinking they did away with the single "|" pipe character, and you're only supposed to use the double pipe "||", but in a certain order. However, I don't want the if statement to pass as true if appPurchased = false, enabled = true, button = photoLibraryBtn OR button = takeVideoBtn.
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
}
As the title says, I just came across a case where if && (AND) and , give the same result in Swift. I tested the code below:
let a = 5
let b = 6
if a<6, b>0 {
print("should be true")
}
if a<6, b<0 {
print("should be false")
}
if a>6, b>0 {
print("should be false")
}
if a>6, b<0 {
print("should be false")
}
It only logs:
should be true
So, the behavior of , is just like &&, am I correct?
They can be used in similar situations but that does not mean they are exactly the same.
Consider:
if (a && b) || c
you cannot write
if (a, b) || c
even a && b || c is different from a, b || c.
if a, b is something like
if a {
if b {
...
}
}
Both expressions have to be evaluated to true but they are still two separate expressions. We shouldn't imagine && there.
Why do we need the , operator?
The operator is needed to combine optional binding with boolean conditions, e.g.
if let a = a, a.isValid() {
becuase && wouldn't help us in such situations.
They're different in that a comma will take the lowest precedence possible. i.e everything else will be executed before the commas are checked.
// (true || false) && false
if true || false, false {
print("I will never be executed")
}
// true || (false && false)
if true || false && false {
print("I am always executed")
}
PORTB = 0b11001011;
PORTC = 0b00111011;
if(PORTB & PORTC)
{
//do something
}
//will this "if" comes out to be true?how compiler will check it?
PORTB = 0b11001011;
PORTC = 0b00111011;
if(PORTB && PORTC)
{
//do something
}
//how this "&&" be compared? if i write "if(PORTB)" than how compiler wil evaluate it?
& does bit and, so
PORTB & PORTC == 0b00001011
converting this to a boolean yields true (non-zero)
&& does boolean and, so PORTB is true (non-zero), and PORTC is true, so
PORTB && PORTC
yields true
So is there a difference? Yes. && uses short-circuiting, while & doesn't. Consider the following:
0 & some_complicated_expression
0 && some_complicated_expression
In the first case, the complicated expression will be evaluated. However, since && uses short-circuiting, the complicated expression is not evaluated in the second case (the expression yields false, irrespective of the result of the complicated expression).