当按下%并且没有数字时,Swift计算器崩溃

时间:2019-05-18 19:16:45

标签: swift

这是我编写的第一个Swift程序。我想从这篇文章中了解两点。

  • 对我编写的代码以及是否有其他更好的方法可以编写此计算器的反馈。

  • 当没有数字时,用户按下%按钮时如何解决崩溃问题

我尝试将其放在if语句和while语句中,以尝试在label.text包含nil的情况下分隔所需的动作

import UIKit

class ViewController: UIViewController {

    var numberOnScreen : Double = 0
    var previousNumber : Double = 0
    var operation = 0
    var characterCount = 0

    @IBOutlet weak var label: UILabel!

    @IBAction func numberPressed(_ sender: UIButton) {            
        label.text = label.text! + String(sender.tag)
        numberOnScreen = Double(label.text!) as! Double
        characterCount = label.text!.count
        if characterCount > 7 {
            label.font = label.font.withSize(30)
        } 
    }

    @IBAction func symbolPressed(_ sender: Any) {
        if (sender as AnyObject).tag == 10 {
            label.text = ""
            numberOnScreen = 0
        } else if (sender as AnyObject).tag == 11 {
            if (label.text?.contains("-"))! {
                label.text = label.text?.replacingOccurrences(of: "-", with: "", options: NSString.CompareOptions.literal, range: nil)
                numberOnScreen = Double(label.text!) as! Double
            } else {
                label.text = "-" + label.text!
                numberOnScreen = Double(label.text!) as! Double
            }
        } else if (sender as AnyObject).tag == 12{
            if label.text == nil {
                label.text = "0"
            }
            else label.text != nil {
                numberOnScreen = label.text! as! Double
                numberOnScreen = numberOnScreen / 100
                label.text = String(numberOnScreen)
            }                
        } else if (sender as AnyObject).tag == 13 {
            operation = 1
            previousNumber = Double(label.text!) as! Double
            label.text = ""
        } else if (sender as AnyObject).tag == 14 {
            operation = 2
            previousNumber = Double(label.text!) as! Double
            label.text = ""
        } else if (sender as AnyObject).tag == 15 {
            operation = 3
            previousNumber = Double(label.text!) as! Double
            label.text = ""
        } else if (sender as AnyObject).tag == 16 {
            operation = 4
            previousNumber = Double(label.text!) as! Double
            label.text = ""
        } else if (sender as AnyObject).tag == 17 {
            if operation == 1 {
                label.text = String(previousNumber/numberOnScreen)
            } else if operation == 2 {
                label.text = String(previousNumber*numberOnScreen)
            } else if operation == 3 {
                label.text = String(previousNumber-numberOnScreen)
            } else if operation == 4 {
                label.text = String(previousNumber+numberOnScreen)
            }
        } else if (sender as AnyObject).tag == 18 {
            if label.text == "" {
                label.text = "0."
            } else if (label.text?.contains("."))! {

            } else  {
                label.text = label.text! + "."
            }
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.            
    }

}

我不断收到

的错误
  

意外发现nil,同时隐式展开一个可选值

尽管有条件绑定

这是我在做的时候

if label.text == nil {
}

尝试在出现nil时不采取任何行动

我正在使用当前代码

  

线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

只需在以下其他人的帮助下进行编辑,便可以解决所有问题。非常感谢!下面是我创建的新代码!

导入UIKit

ViewController类:UIViewController {

var  displayInt = true
var numberOnScreen : Double = 0
var previousNumber : Double = 0
var operation = 0
var characterCount = 0
var display = ""


@IBOutlet weak var label: UILabel!

@IBAction func numberPressed(_ sender: UIButton) {

    //sets uilabel number to a double called numberOnScreen
    label.text = label.text! + String(sender.tag)
    numberOnScreen = Double(label.text!)!

    //checks for 7 characters and changes font size of label to allow
    //more characters on the screen
    displayFont()
}

@IBAction func symbolPressed(_ sender: UIButton) {
    //clear button resets values
    if (sender).tag == 10 {
        label.text = ""
        numberOnScreen = 0
        characterCount = 0
        //resets font to original size after decimal
        label.font = label.font.withSize(75)
    //positive/negative button
    } else if (sender).tag == 11 {
        //checks for a negative and removes it and sets the number back to positive
        if (label.text?.contains("-"))! {
            label.text = label.text?.replacingOccurrences(of: "-", with: "", options: NSString.CompareOptions.literal, range: nil)
            numberOnScreen = -numberOnScreen
        } else { //else make the number negative
            label.text = "-" + label.text!
            numberOnScreen = -numberOnScreen
        }
    //percentage function
    } else if (sender).tag == 12{
        //if user presses this button first, it avoids a nil by
        //setting the screen to show 0
        if numberOnScreen == 0 {
            label.text = "0"
        }
        //ensures that nil cannot be used to run this function
        else if label.text != nil {
            numberOnScreen = numberOnScreen / 100
            label.text = String(numberOnScreen)
        }
    //division function gives value to previous number and resets screen value
    } else if (sender).tag == 13 {
        operation = 1
        previousNumber = numberOnScreen
        label.text = ""
    //multiplication function function gives value to previous number and resets screen value
    } else if (sender).tag == 14 {
        operation = 2
        previousNumber = numberOnScreen
        label.text = ""
    //subtraction function gives value to previous number and resets screen value
    } else if (sender).tag == 15 {
        operation = 3
        previousNumber = numberOnScreen
        label.text = ""
    //addition function gives value to previous number and resets screen value
    } else if (sender).tag == 16 {
        operation = 4
        previousNumber = numberOnScreen
        label.text = ""
    //equals function
    } else if (sender).tag == 17 {
        //displays integer values if no decimal is used to keep cleaner display
        if displayInt == true {
            if operation == 1 { //math of functions based upon operation number
                label.text = String(Int(previousNumber/numberOnScreen))
            } else if operation == 2 {
                label.text = String(Int(previousNumber*numberOnScreen))
            } else if operation == 3 {
                label.text = String(Int(previousNumber-numberOnScreen))
            } else if operation == 4 {
                label.text = String(Int(previousNumber+numberOnScreen))
            }
        } else { //uses double if decimal is used
            if operation == 1 { //math of functions based upon operation number
                label.text = String(previousNumber/numberOnScreen)
                displayFont()
            } else if operation == 2 {
                label.text = String(previousNumber*numberOnScreen)
                displayFont()
            } else if operation == 3 {
                label.text = String(previousNumber-numberOnScreen)
                displayFont()
            } else if operation == 4 {
                label.text = String(previousNumber+numberOnScreen)
                displayFont()
            }
        }
    //decimal point button
    //checks for no value to start off the number as 0.
    } else if (sender).tag == 18 {
        if label.text == "" {
            label.text = "0."
            displayInt = false
        //ensures only one decimal may be placed
        } else if (label.text?.contains("."))! {

        } else { //inserts decimal
            label.text = label.text! + "."
            displayInt = false
        }
    }

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

}
//checks for 7 characters and changes font size of label to allow
//more characters on the screen
func displayFont() {
    characterCount = label.text!.count
    if characterCount > 7 {
        label.font = label.font.withSize(30)
    }
}

}

0 个答案:

没有答案