在展开错误时声明或意外为零;不确定哪个

时间:2016-03-09 07:56:03

标签: ios swift if-statement textbox optional

*快速注意,图片将有助于解释我的情况。所以此刻,我只是添加了暨。学分和GPA文本字段到我的GPA计算器应用程序。这两个字段是可选的,而下面的其他4个字段则不是。以下是我的大部分代码(请原谅目前的冗余)。

         if CumCredits == "" || CumGPA == ""
        {
            let credit1 = Float(Credits1TF.text!)!
            let credit2 = Float(Credits2TF.text!)!

            let grade1 = Float(Grade1TF.text!)!
            let grade2 = Float(Grade2TF.text!)!

            let firstgrade = credit1 * grade1
            let secondgrade = credit2 * grade2

            let allcredits = credit1 + credit2
            let allgrades = firstgrade + secondgrade

            let finalgrade = round((allgrades / allcredits) * 10000)/10000


            GPALabel.text = "Your Semester GPA is: \(finalgrade)"
            GPALabel.hidden = false

            Class1TF.resignFirstResponder()
            Class2TF.resignFirstResponder()
            Credits1TF.resignFirstResponder()
            Credits2TF.resignFirstResponder()
            Grade1TF.resignFirstResponder()
            Grade2TF.resignFirstResponder()


            }
            else {
            let credit1 = Float(Credits1TF.text!)!
            let credit2 = Float(Credits2TF.text!)!

            let grade1 = Float(Grade1TF.text!)!
            let grade2 = Float(Grade2TF.text!)!

            let firstgrade = credit1 * grade1
            let secondgrade = credit2 * grade2

            let allcredits = credit1 + credit2
            let allgrades = firstgrade + secondgrade

            let finalgrade = round((allgrades / allcredits) * 10000)/10000

            Class1TF.resignFirstResponder()
            Class2TF.resignFirstResponder()
            Credits1TF.resignFirstResponder()
            Credits2TF.resignFirstResponder()
            Grade1TF.resignFirstResponder()
            Grade2TF.resignFirstResponder()
            CumCredits.resignFirstResponder()
            CumGPA.resignFirstResponder()

            let CCredits = Float(CumCredits.text!)!
            let CGPA = Float(CumGPA.text!)!

            //top math
            let LastGrade = CCredits * CGPA
            let MegaCredits = allcredits + CCredits
            let MegaGPA = allgrades + LastGrade
            let MegaEverything = (round((MegaGPA / MegaCredits) * 10000)/10000) - 0.0001

            CumTotal.hidden = false
            CumTotal.text = "Your expected cumulative GPA is: \(MegaEverything)"
            GPALabel.text = "Your Semester GPA is: \(finalgrade)"
            GPALabel.hidden = false

            Class1TF.resignFirstResponder()
            Class2TF.resignFirstResponder()
            Credits1TF.resignFirstResponder()
            Credits2TF.resignFirstResponder()
            Grade1TF.resignFirstResponder()
            Grade2TF.resignFirstResponder()
            CumCredits.resignFirstResponder()
            CumGPA.resignFirstResponder()

        }

此刻(据我所知),如果暨。 Credits和GPA保持不变,我的语句的顶部执行,如果它们不为空,则执行else语句。

所以我的问题......如果我为每个字段输入一个值,那么所有单词和我都没有问题。但是,如果我离开暨。积分或GPA字段为空,我的程序崩溃,我在打开一个可选值"时得到了典型的"意外发现的nil;处理

 let CCredits = Float(CumCredits.text!)!
 let CGPA = Float(CumGPA.text!)!

我已经尝试了许多可能的解决方案:重新编写代码并重新编写代码,我似乎无法得到它。你能建议什么?

任何帮助将不胜感激!

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试强制解包可选项时会发生此错误,因此请先查看每个"!"使用过,你可能会发现其中一个是零。

修改的 在评论中查看和讨论之后,似乎你的if语句应该是

!CumCredits.characters.isEmpty || !CumGPA.characters.isEmpty

在这种情况下可能发生的事情是您的字段中的值是空字符串,因此尝试从条目字符串创建浮点值将导致nil值。

一种解决方案是在函数顶部放置一个保护语句,以便永远不会运行导致错误的代码:

guard let credits1Text = Credits1TF.text, 
          credits2Text = Credits2TF.text, 
          grade1Text = Grade1TF.text, 
          grade2Text = Grad2TF.text
where !credits1Text.characters.isEmpty && !credits2Text.characters.isEmpty 
     && !grade1Text.characters.isEmpty && !grade2Text.characters.isEmpty else {
   *handle case where float conversion won't work*
}
*continue on with normal code, using verified values created in guard*

答案 1 :(得分:0)

问题在于if语句。

$('.main-section').each(function(){
  var ul = $(this).find('.sub-sections-list');
  $(this).find('h3').each(function(){
    ul.append('<li><a href="#">'+$(this).text()+'</a></li>');
  });
});

这样,如果我的文本字段为空,则会传递我代码的第一部分。