Search code examples
swiftsegueuisegmentedcontroluialertcontroller

Xcode skips if else statement


my if else statement checks whether some text fields are empty and if so pops an alert. However xcode even if going through everything, moves on to other functions.

There is an if statement which checks the value of a segmented control and accordingly checks some text fields.

@IBAction func calc(_ sender: Any) {
    
    // Check if dilution text field is empty
    let dilutiontext = self.dilution.text
    if (dilutiontext?.isEmpty ?? true) {
        Alert.showAlert(on: self, with: "Empty Fields", message: "Dilution field is empty")
    }
    if choose.selectedSegmentIndex == 0 {
        
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true {
            Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-4 fields should not be empty")
        } else {
            performSegue(withIdentifier: "turner", sender: self)
        }
    } else {
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true {
            Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-8 fields should not be empty")
        } else {
            performSegue(withIdentifier: "turner", sender: self)
        }
    }
    
}

I have another file alert.swift which controls the alerts:

import Foundation
import UIKit

struct Alert {
    public static func showAlert(on vc: UIViewController, with title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        vc.present(alert, animated: true)
    }
}

EDIT:

Previously self.dilution.text?.isEmpty and now let dilutiontext = self.dilution.text with dilutiontext?isEmpty

I commented out the prepare for segue function and surprisingly the alerts started working. I still need that function and the alerts working though. Here is the function:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    var vc = segue.destination as! SecondViewController
    
    if choose.selectedSegmentIndex == 0 {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.dil = Int(dilution.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)

    } else {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.n5 = Int(number5.text!)!
        vc.n6 = Int(number6.text!)!
        vc.n7 = Int(number7.text!)!
        vc.n8 = Int(number8.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)
        vc.dil = Int(dilution.text!)!
    }

}

When I run it, instead of showing the alerts (which check if a text field is empty) it continues to the segue function and displays Unexpectedly found nil while unwrapping an Optional value, which is expected


Solution

  • Clearly the alerts were skipped if one of the if conditions in the segue function was true. So if there was something that would initially make the statements false and then after going through the alerts it would make them true, the problem would be solved.

    Therefore I made two more functions each for the if and if else statements in the segue func.

    func option1() -> Bool {
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true || !(self.number5.text?.isEmpty ?? true) || !(self.number6.text?.isEmpty ?? true) || !(self.number7.text?.isEmpty ?? true) || !(self.number8.text?.isEmpty ?? true) {
            return false
        } else {
            return true
        }
    }
    
    func option2() -> Bool {
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true {
            return false
        } else {
            return true
        }
    }
    

    which checked if all the conditions were true and if so returned true, so that the program could move on to the segue func.

    The segue would check whether the conditions were true, if not, it would go through the alerts-therefore making the option1() or option2() return true- and so the if conditions in the segue func would be true for the program to continue.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        var vc = segue.destination as! SecondViewController
    
        if option1() == true {
            vc.n1 = Int(number1.text!)!
            vc.n2 = Int(number2.text!)!
            vc.n3 = Int(number3.text!)!
            vc.n4 = Int(number4.text!)!
            vc.dil = Int(dilution.text!)!
            vc.cn = Int(choose.selectedSegmentIndex)
    
        } else if option2() == true {
            vc.n1 = Int(number1.text!)!
            vc.n2 = Int(number2.text!)!
            vc.n3 = Int(number3.text!)!
            vc.n4 = Int(number4.text!)!
            vc.n5 = Int(number5.text!)!
            vc.n6 = Int(number6.text!)!
            vc.n7 = Int(number7.text!)!
            vc.n8 = Int(number8.text!)!
            vc.cn = Int(choose.selectedSegmentIndex)
            vc.dil = Int(dilution.text!)!
        }
    
    }