-1

I have to make some calculus based on two dates and the user says if it should calc the initial month or not.

My code is like this:

 let qtMeses = getMonthDifference( dateaq!, d2: datebase! )
    if ( proprioMesSwitch.on  ){
      let qtMeses = qtMeses + 1
    }

The routine count how many month between the two dates and check to see if the user checked the initial month option.

Say the qtMeses variable has a value of 20 after calling the getMonthDifference procedure. After it tests the proprioMesSwitch and executes the let qtMeses = qtMeses + 1 line but even though the result after that is still the same 20.

Nimantha
  • 6,405
  • 6
  • 28
  • 69

2 Answers2

2

With let ... you define a new variable qtMeses, hiding the outer one.

Use:

var qtMeses = getMonthDifference( dateaq!, d2: datebase! )
if (proprioMesSwitch.on) {
   qtMeses = qtMeses + 1
}
Mario Zannone
  • 2,843
  • 12
  • 18
0

Even though I have declared a variable qtMeses with var qtMeses I used let qtMeses to assign a value to this variable.

But because of let qtMeses swift changed my var into a constant thats why I couldnt change its value anymore.

qtMeses = getMonthDifference( dateaq!, d2: datebase! )
if ( proprioMesSwitch.on  ){
  qtMeses = qtMeses + 1
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69