Why breakpoint breaks at else block in 'if else' condition - swift

I am trying to figure out, why, if I put breakpoints on if and on else line, why my if else {} condition breaks on else if the condition was true in if block?
I am using realm but I do not think, that is an issue.
//Check if Object already exists in database
if !RealmService.shared.ifPortfolioExists(name: portfolio.name){//Breakpoint on this line which is true
//Create portfolio
RealmService.shared.create(portfolio)
//Assign portfolio to transaction
transaction.portfolio = portfolio
//Create transaction
RealmService.shared.create(transaction)
}else{//Breakpoint on this line
//Assign portfolio to transaction
transaction.portfolio = portfolio
//Create transaction
RealmService.shared.create(transaction)
}
Am I working out of my mind or am I just stupid? Can please someone explain me this.

The compiler reads them to see if it should proceed into the following block of code. A breakpoint inside of the if will trigger when true and else when false
Do this to better understand where your breakpoints take effect:
if x != y {
print("if breakpoint")//set breakpoint here
} else {
print("else breakpoint")//set breakpoint here
// you should see that the breakpoint doesn't fire here
}

Related

Counter for re-opened cases - Salesforce

I'm traying to create a trigger that count each time a case is re-opened.
What I need is that changing the Status "Cerrado" to "Asignado" add +1
I've this trigger but it doesn't working at all.
Reperturas__c a type number and in the formula I've a 0.
"Cerrado" close the case.
Any solution guys?. Thanks!
trigger caseReOpen on Case (before update) {
for(Case c:trigger.new){
if(trigger.Oldmap.get(c.Id).Status=='Cerrado'){
c.Reaperturas__c ++;
}
}
}
Should work, weird. How's this?
trigger caseReOpen on Case (before update) {
for(Case c: trigger.new){
Case old = trigger.oldMap.get(c.Id);
if(c.Status == 'Asignado' && old.Status == 'Cerrado'){
++c.Reaperturas__c;
}
}
}
Compiled and saved ok? Can you put field history tracking on it, maybe you have something else that resets the counter? Are the actual picklist values like that or are these just labels? You need to use API values in Apex so if they're in English and you just have labels translated - your code won't work.
What if you base the check on !c.IsClosed && old.IsClosed ? It's a computed checkbox, can't be edited directly but it's bit more portable... If in future you add more statuses counting as closing (closed completed, closed cancelled etc?). https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_case.htm

Find out whether function returns true or false

I am struggling to make form validation work and it looks like the main problem is placeOrder function (other function which sit inside this functions work just fine). The function behaves in a strange way (gives alert only if the first input field (message) does not confirm to the test conditions, while if I leave the others blank it does not show alert). I was trying to find the reason for that by putting each part of the main if statement (which are divided by AND operators) in this test:
if(validateLenghtData(5, 32, form["message"], form["message_help"])) {
console.log("validation passed");
} else {
console.log("validation failed");
}
This gave me nothing because it only returns false ("validation failed") to console, while it outputs nothing if I enter valid text to the input. Where can be the bug and how can I test each part of big if statement ( like validatedZipCode(form["zipcode"], form["zipcode_help"])) in order to find out which part gives me false.
function placeOrder(form) {
// first check whether all fields are filled with valid data
// form["message"] and other simular arguments are part of form object and are passed from submit form (look it)
if (validateLenghtData(5, 32, form["message"], form["message_help"]) &&
validatedZipCode(form["zipcode"], form["zipcode_help"]) &&
validateNonEmptyField(form["date"], form["date_help"]) &&
validateNonEmptyField(form["name"], form["name_help"]) &&
validateNonEmptyField(form["phone"], form["phone_help"]) &&
validateNonEmptyField(form["email"], form["email_help"])) {
// everything is ok, submit the order to the server
form.submit();
} else {
alert ("You need to feel all fields correctly");
}
if(validateLenghtData(5, 32, form["message"], form["message_help"])) {
console.log("validation passed");
} else {
console.log("validation failed");
}
}

understanding why swift code won't work correctly

I'm new to coding and currently teaching myself swift using swift playgrounds on the iPad. My code runs and completes the puzzle but it continues to loop and I don't know why. I can't find any way to correct this code. Although I have found videos on YouTube with various code written differently. I don't just want to copy it though. I want to understand why this isn't working. I can send a video of the puzzle if needed.
while !isOnGem || !isOnClosedSwitch {
moveForward()
if isBlocked && !isBlockedRight {
turnRight()
}
if isBlocked && isBlockedRight {
turnLeft()
}
if isOnGem {
collectGem()
}
if isOnClosedSwitch {
toggleSwitch()
}
}
Without any other information regarding the functions in each of your if blocks, I'd say that it is due to your boolean values for isOnGem and isOnClosedSwitch. If the function collectGem() does not change the value of isOnGem to the opposite of what it was initially set to (true or false) and toggleSwitch() doesn't change the value of isOnClosedSwitch to the opposite of it's original value then you will be stuck in the loop. Since the loop will run "while" at least one of those values remain unchanged.
I believe adding a isOnGem = false and isOnClosedSwitch = false to their respective if blocks will be the solution.
You are missing the exit condition. while !isOnGem || !isOnClosedSwitchwill continue to loop as long as either condition is true, therefore your exit condition will be having both values set to false.
Note that both Booleans are inverted in your check so to make both conditions false you have to set the Booleans to true.
Since you code runs and yet does not exit to loop, you will want to check for changes to isOnGem and isOnClosedSwitch there might be one of the two that is always false resulting in the loop not exiting or the function that runs after each checks might have reset them to false
check for code like:
func collectGem(){
...
isOnGem = false
...
}
or one of the functions might not even have run, you can log each function like :
func toggleSwitch() {
print("toggleSwitchRunning")
}
and if "toggleSwitchRunning" did not print into the console, check that the condition that set isOnClosedSwitch to true is working properly

code to destroy zend session gets executed although its not allowed by if else and switch case conditions

$getId = $this->getRequest()->getParam('id'); // 1 is coming from url
$id_from_cart = 1;
if($getId != $id_from_cart) {
echo "unset session";
Zend_Session::namespaceUnset('cart');
} else {
echo "dont unset";
}
When I use $getId = 1 as a static value, its working fine but if I use $this->getRequest()->getParam('id') as its value which is also 1 then although it goes in else condition in both the cases but it unsets the session (code written in if condition). How is that possible, code to destroy session has been written in if condition. I have tried many things but couldn't reach anywhere. Any suggestion could be a great help. I have tried switch case, in_array and if else.
Try this
$getId = (int) $this->getRequest()->getParam('id');

Boolean logic failure

I am having a strange problem with boolean logic. I must be doing something daft, but I can't figure it out.
In the below code firstMeasure.isInvisibleArea is true and measureBuffer1 is nil.
Even though test1 is evaluating to NO for some reason it is still dropping into my if statement.
It works ok if I use the commented out line.
Any idea why this happens?
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1 == nil;
BOOL test1 = measureBuffer1 == nil && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 == nil && !firstMeasure.isInVisibleArea)
//if (measureBufferNil && !firstVisible)
{
//do some action
}
Update 1:
I isolated the problem to !firstMeasure.isInVisibleArea as I've entirely taken on the measureBuffer bit.
Inside isInVisible area is a small calculation (it doesn't modify anything though), but the calculation is using self.view.frame. I am going take this out of the equation as well and see what happens. My hunch is that self.view.frame is changing between the two calls to isInVisibleArea.
Update 2:
This is indeed the problem. I have added the answer in more detail below
When in doubt, you should fully parenthesize. Without looking up the precedence rules, what I think what is happening is that = is getting higher precedence than == or &&. So try:
BOOL test1 = ((measureBuffer1 == nil) && !firstMeasure.isInVisibleArea);
While you certainly can parenthesize, you should also know that nil objects evaluate to boolean NO and non-nil objects evaluate to boolean YES. So you could just as easily write this:
BOOL firstVisible = firstMeasure.isInVisibleArea;
BOOL notFirstVisible = !(firstMeasure.isInVisibleArea);
BOOL measureBufferNil = measureBuffer1;
BOOL test1 = !measureBuffer1 && !firstMeasure.isInVisibleArea;
BOOL test2 = measureBufferNil && !firstVisible;
if (measureBuffer1 && !firstMeasure.isInVisibleArea) {
//do some action
}
You would end up with the same results. I agree with GoatRider, though. It's always far better to parenthesize your conditional expressions to clarify what you really want to happen than it is to rely on the language's operator precedence to do it for you.
If test1 is evaluating to NO as you say, then drop test1 into the if statement:
if(test1){
//see if this executes?
}
See what that does.
My hunch was correct, it is related to the view frame changing between calls to firstMeasure.isInVisible area.
This whole routine is called in response to the view moving. I think I need to grab the value of firstMeasure.isInVisibleArea at the start of the method and use that value throughout.
Phew. Boolean logic isn't broken. All is right with the world.
Thanks for all your input