Hello!

I have a variable.

var variant: Int=0 

There are functions that should change the value after pressing certain buttons.

 @IBAction func pohudenie(_ sender: Any) { variant = 1 print(variant) } @IBAction func podderzhanie(_ sender: Any) { variant = 2 print(variant) } @IBAction func nabor(_ sender: Any) { variant = 3 print(variant) } 

After that comes the next button, in which the variable should be defined via the if statement and perform one of the actions.

 @IBAction func result(_ sender: Any) { if variant == 1 { kall2=(kall-(kall/100)*25) } if variant == 2 { kall2=(kall-(kall/100)*20) } else{ kall2=kall } } 

I dropped the extra code. The problem is that in the result function, the variant variable is either not determined, or the variable does not change to the desired one in the first three functions. As a result, the third action from else is performed. How to solve this problem?

As I understand it, this change of variable remains within those buttons.

I add all the code

 import UIKit class ViewController: UIViewController { @IBOutlet weak var text1: UITextField! @IBOutlet weak var text2: UITextField! @IBOutlet weak var sextype: UISegmentedControl! @IBOutlet weak var traning: UISegmentedControl! @IBOutlet weak var activ: UISegmentedControl! @IBOutlet weak var resultlabel: UILabel! func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } var variant: Int = 0; @IBAction func pohudenie(_ sender: Any) { variant = 1 print(variant) } @IBAction func podderzhanie(_ sender: Any) { variant = 2 print(variant) } @IBAction func nabor(_ sender: Any) { variant = 3 print(variant) } let height = 170 @IBAction func result(_ sender: Any) { var kall: Double=0 var kall2: Double=0 let activity = [1.2, 1.38, 1.56, 1.73, 1.95] let selectedActiv = activity[activ.selectedSegmentIndex] let factor = [0.9, 0.95, 1, 1.03, 1.06, 1.09, 1.11, 1.13] let selectedFactor = factor[traning.selectedSegmentIndex] if let age = Int(text1.text!) { if let weight = Int(text2.text!) { switch sextype.selectedSegmentIndex { case 0: kall=(Double(selectedFactor) * (Double(selectedActiv) * (66 + (13.7 * Double(weight)) + (5 * Double(height)) - (6.8 * Double(age))))) case 1: kall=(Double(selectedFactor) * (Double(selectedActiv) * (665 + (9.6 * Double(weight)) + (1.8 * Double(height)) - (4.7 * Double(age))))) default: kall = 0 } } } switch variant { case 1: kall2=(kall-(kall/100)*25) case 2: kall2=(kall-(kall/100)*20) case 3: kall2=kall default: kall2=kall } let belki = 0.31 let zhir = 0.12 let uglevod = 0.57 let belki1 = 3.8 let zhir1 = 9.3 let uglevod1 = 4.1 var belki2: Double=0 var zhir2: Double=0 var uglevod2: Double=0 belki2=(kall2*belki)/belki1 zhir2=(kall2*zhir)/zhir1 uglevod2=(kall2*uglevod)/uglevod1 UIApplication.shared.keyWindow!.endEditing(true) resultlabel.text = "Вы должны потреблять \(Int(kall2)) килокалорий\nИз которых: \nБелков: \(Int(belki2)) g \nЖиров: \(Int(zhir2)) g \nБелков: \(Int(uglevod2)) g" } override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.view.backgroundColor = .clear // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

    4 answers 4

    The problem is your check. Read the move:

     if variant == 1 { kall2=(kall-(kall/100)*25) } if variant == 2 { kall2=(kall-(kall/100)*20) } else{ kall2=kall } 

    if variant is 1, we perform the action. If variant is equal to 2, we perform the action, otherwise (if variant is equal to any number except 2), perform the action. Those. kall2 = kall will always work, except variant == 2

    Change to something like:

     switch variant { case 1: //одно действие case 2: //второе case 3: //третье default: //все остальное } 
    • I tried your option, it still does not work, and if you put one of the case equal to zero, then it will perform the action. From this I conclude that the variant in any of the cases I have is equal to 0. - Oleg Meditskiy

    The problem was solved by transferring the variant variable to the class. My mistake was that I did not immediately provide you with all the code, in which it is clear that the variable is inside the class.

      Maybe the problem is in the kall or kall2 variable? Check the connection of the buttons, maybe some have been unhooked. Just from what you showed is not at all clear what the problem may be. It seems you have determined everything correctly and is changing, so explanations would be very helpful.

      • added the code above, the variables kall and kall2 do not cause any problems) - Oleg Meditskiy

      Try the most trivial way, add observer to the kal variable:

       { didSet { debugPrint("KAL CHANGED: \(self.kal)") } } 

      And according to the results, see where it incorrectly changes.

      • (if it is announced immediately with a value, then insert the code immediately after the declaration of the value, otherwise, after specifying the type of variable (Int, etc.)) - Morgan Freewalker