problem with a series of if statements

My app has a series of if statements. some of them a executed even when the condition is not met.

 if viewModel.npH > 7.45 {
        diagnos.resultado1 = "alcalemia"
        print ("1")
        }
      if viewModel.npCO2 < 35 {
        diagnos.resultado2 = "alcalosis respiratoria"
        print ("2")
        }
      if viewModel.nHCO3 > 26 {
        diagnos.resultado3 = "alcalosis metabólica"
        print ("3")
      }
       
      if viewModel.npH < 7.35 {
        diagnos.resultado1 = "acidemia"
        print ("4")
      }
      if viewModel.npCO2 > 45 {
        diagnos.resultado2 = "acidosis respiratoria"
        print ("5")
      }
      if viewModel.nHCO3 < 22 {
        diagnos.resultado3 = "acidosis metabólica"
        print ("6")
      }

For instance when i run it with viewModel.nph = 7.4, viewModel.npCO2 = 40 and viewModel.nHCO3 = 23.9 It prints "4", "5", "6" even though none of the conditions are met. I'm obviously doing something wrong but I can't understand what is it I' m doing wrong

Accepted Reply

Problem solved. Was missing a parenthesis so it was executing the closure due to the || part of the statement

Replies

Definitely unexpected based on the limited code you posted. I’d add more sanity checking by also printing the value of the tested value within each block:

if viewModel.npH < 7.35 {
    diagnos.resultado1 = "acidemia"
    print("4", viewModel.npH)
}

Does this turn up any incorrect assumptions?

  • Thanks Scott, I'll check printing the values and report back

  • Now it refuses to print anything it returns the following :

    2022-05-31 12:26:16.459942-0300 NoziMVVM[69131:1573765] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want.  Try this:  (1) look at each constraint and try to figure out which you don't expect;  (2) find the code that added the unwanted constraint or constraints and fix it.  (   "<NSLayoutConstraint:0x600001e49310 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x1488183f0]-(6)-[_UIModernBarButton:0x1488109d0'noziCalc']  (active)>",   "<NSLayoutConstraint:0x600001e49360 'CB_Trailing_Trailing' _UIModernBarButton:0x1488109d0'noziCalc'.trailing <= _UIButtonBarButton:0x146f0a9b0.trailing  (active)>",   "<NSLayoutConstraint:0x600001e56620 'UINav_static_button_horiz_position' _UIModernBarButton:0x1488183f0.leading == UILayoutGuide:0x6000004789a0'UIViewLayoutMarginsGuide'.leading  (active)>",   "<NSLayoutConstraint:0x600001e56670 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x146f0a9b0]-(6)-[UILayoutGuide:0x6000004788c0'UINavigationBarItemContentLayoutGuide']  (active)>",   "<NSLayoutConstraint:0x600001e5c7d0 'UINavItemContentGuide-trailing' UILayoutGuide:0x6000004788c0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x146f093e0.trailing  (active)>",   "<NSLayoutConstraint:0x600001e42d00 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x146f093e0.width == 0  (active)>",   "<NSLayoutConstraint:0x600001e5c410 'UIView-leftMargin-guide-constraint' H:|-(8)-  (active, names: '|':_UINavigationBarContentView:0x146f093e0 )>" )

    Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x600001e49310 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x1488183f0]-(6)-[_UIModernBarButton:0x1488109d0'noziCalc']  (active)>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2022-05-31 12:26:16.460578-0300 NoziMVVM[69131:1573765] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want.  Try this:  (1) look at each constraint and try to figure out which you don't expect;  (2) find the code that added the unwanted constraint or constraints and fix it.  (   "<NSLayoutConstraint:0x600001e492c0 'BIB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x1488183f0]  (active, names: '|':_UIButtonBarButton:0x146f0a9b0 )>",   "<NSLayoutConstraint:0x600001e56620 'UINav_static_button_horiz_position' _UIModernBarButton:0x1488183f0.leading == UILayoutGuide:0x6000004789a0'UIViewLayoutMarginsGuide'.leading  (active)>",   "<NSLayoutConstraint:0x600001e56670 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x146f0a9b0]-(6)-[UILayoutGuide:0x6000004788c0'UINavigationBarItemContentLayoutGuide']  (active)>",   "<NSLayoutConstraint:0x600001e5c7d0 'UINavItemContentGuide-trailing' UILayoutGuide:0x6000004788c0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x146f093e0.trailing  (active)>",   "<NSLayoutConstraint:0x600001e42d00 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x146f093e0.width == 0  (active)>",   "<NSLayoutConstraint:0x600001e5c410 'UIView-leftMargin-guide-constraint' H:|-(8) )

    Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x600001e492c0 'BIB_Leading_Leading' H:|-(0)-[_UIModernBarButton:0x1488183f0]  (active, names: '|':_UIButtonBarButton:0x146f0a9b0 )>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2022-05-31 12:26:16.467619-0300 NoziMVVM[69131:1573765] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want.  Try this:  (1) look at each constraint and try to figure out which you don't expect;  (2) find the code that added the unwanted constraint or constraints and fix it.  (   "<NSLayoutConstraint:0x600001e55f40 UIView:0x14881cdf0.trailing == _UIBackButtonMaskView:0x14881c690.trailing  (active)>",   "<NSLayoutConstraint:0x600001e49900 'Mask_Trailing_Trailing' _UIBackButtonMaskView:0x14881c690.trailing == _UIButtonBarButton:0x146f0a9b0.trailing  (active)>",   "<NSLayoutConstraint:0x600001e49a40 'MaskEV_Leading_BIB_Trailing' H:[_UIModernBarButton:0x1488183f0]-(0)-[UIView:0x14881cdf0]  (active)>",   "<NSLayoutConstraint:0x600001e56620 'UINav_static_button_horiz_position' _UIModernBarButton:0x1488183f0.leading == UILayoutGuide:0x6000004789a0'UIViewLayoutMarginsGuide'.leading  (active)>",   "<NSLayoutConstraint:0x600001e56670 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x146f0a9b0]-(6)-[UILayoutGuide:0x6000004788c0'UINavigationBarItemContentLayoutGuide']  (active)>",   "<NSLayoutConstraint:0x600001e5c7d0 'UINavItemContentGuide-trailing' UILayoutGuide:0x6000004788c0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x146f093e0.trailing  (active)>",   "<NSLayoutConstraint:0x600001e42d00 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x146f093e0.width == 0  (active)>",   "<NSLayoutConstraint:0x600001e5c410 'UIView-leftMargin-guide-constraint' H:|-(8)- (active, names: '|':_UINavigationBarContentView:0x146f093e0 )>" )

    Will attempt to recover by breaking constraint  <NSLayoutConstraint:0x600001e55f40 UIView:0x14881cdf0.trailing == _UIBackButtonMaskView:0x14881c690.trailing  (active)>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2022-05-31 12:26:21.091643-0300 NoziMVVM[69131:1573765] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead. 2022-05-31 12:26:21.093458-0300 NoziMVVM[69131:1573765] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead.

Add a Comment

The errors you shows are just layout errors (constraints), nothing to do with your initial problem.

when i run it with viewModel.nph = 7.4, viewModel.npCO2 = 40 and viewModel.nHCO3 = 23.9 It prints "4", "5", "6" 

Are you sure that's the print you get ? Isn't it 2, 4, 6 instead ?

Are the var initialised as:

func diag() {
      viewModel.npH = Double(Float(viewModel.pH) ?? 0)
      viewModel.npCO2 = Double(Float(viewModel.pCO2) ?? 0)
      viewModel.nHCO3 = Double(Float(viewModel.HCO3) ?? 0)

as defined in a previous post ?

If you get 2, 4, 6 that would mean that all values are 0 at first run.

But we miss some information to be sure of the conclusion.

So please instrument code with print, such as:

      if viewModel.npH > 7.45 {
        diagnos.resultado1 = "alcalemia"
        print ("1", viewModel.npH)
        }
      if viewModel.npCO2 < 35 {
        diagnos.resultado2 = "alcalosis respiratoria"
        print ("2", viewModel.npCO2)
        }
else if viewModel.npH > 7.45 && diagnos.resultado3 == "alcalosis metabólica" && (viewModel.expectALCPCO2 + 0.5) < viewModel.npCO2 || viewModel.npCO2 > 55 {
        diagnos.resultado4 = "acidosis respiratoria asociada"
//        diagnos.resultado2 = ""
        diagnos.valorasociado1 = "PCO2 esperada: \(viewModel.expectALCPCO2)"
        print ("8.2" , viewModel.npH)
      }

it printed 8.2 7.099999904632568

even though nph = 7.0999 it executed the closure

  • any help appreciated

Add a Comment

Problem solved. Was missing a parenthesis so it was executing the closure due to the || part of the statement