【问题标题】:Xcode skips if else statementXcode 跳过 if else 语句
【发布时间】:2020-08-06 12:06:57
【问题描述】:

我的 if else 语句检查某些文本字段是否为空,如果是则弹出警报。然而,xcode 即使经历了一切,也会转移到其他功能。

有一个 if 语句检查分段控件的值并相应地检查一些文本字段。

@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)
        }
    }
    
}

我有另一个控制警报的文件 alert.swift:

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)
    }
}

编辑:

以前的 self.dilution.text?.isEmpty 现在让稀释文本 = self.dilution.text 和稀释文本?isEmpty

我注释掉了准备转场功能,令人惊讶的是,警报开始起作用了。我仍然需要该功能并且警报仍然有效。这是函数:

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!)!
    }

}

当我运行它时,它不会显示警报(检查文本字段是否为空),而是继续执行 segue 函数并在展开可选值时显示 Unexpectedly found nil,这是预期的

【问题讨论】:

    标签: swift segue uisegmentedcontrol uialertcontroller


    【解决方案1】:

    显然“if”和“else if”条件都不成立。添加

    let dilutiontext = self.dilution.text
    let celltext = self.cell.text
    

    然后设置断点并检查值。

    【讨论】:

    • 感谢您的帮助。我确实检查了 if 语句是否正确,并且我还对所有 if、else if 和 else 使用了警报,但仍然没有出现。仍在尝试找出解决方案
    【解决方案2】:

    如果 segue 函数中的 if 条件之一为真,则显然会跳过警报。因此,如果有什么东西最初会使这些陈述为假,然后在通过警报后它会使它们成为真的,那么问题就会得到解决。

    因此,我为 segue func 中的 if 和 if else 语句分别制作了两个函数。

    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
        }
    }
    

    它检查所有条件是否为真,如果是则返回真,以便程序可以继续执行 segue 函数。

    segue 将检查条件是否为真,如果不是,它将通过警报——因此使 option1() 或 option2() 返回 true——因此 segue func 中的 if 条件对于程序继续。

    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!)!
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多