Swift应用程序崩溃

时间:2016-03-18 04:32:08

标签: ios xcode swift

我正在尝试创建一个应用程序,用户输入多个季度,一些硬币,一些镍币和一些便士。然后,我想通过显示他们以美元输入的内容以及剩下的任何更改来输出所有内容。我有可以创建所需输出的数学代码。我唯一的问题是输入输入后我无法让我的UIButton Calculate执行代码。我有4个文本字段(每种类型的硬币一个)。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var quarterNum: UITextField!
@IBOutlet weak var dimeNum: UITextField!
@IBOutlet weak var nickelNum: UITextField!
@IBOutlet weak var pennyNum: UITextField!
@IBOutlet weak var calculate: UIButton!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var resultOutputLabel: UILabel!

var dollarAmount: Int = 0
var quarterAmount: Int = 0
var dimeAmount: Int = 0
var nickelAmount: Int = 0
var pennyAmount: Int = 0

var quarters: Int = 0
var dimes: Int = 0
var nickels: Int = 0
var pennies: Int = 0


@IBAction func exitButton(sender: UIButton) {
    exit(0)
}
override func viewDidLoad() {
    super.viewDidLoad()
    initializeTextFields()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func initializeTextFields()
{
    quarterNum.delegate = self
    quarterNum.keyboardType = UIKeyboardType.NumberPad

    dimeNum.delegate = self
    dimeNum.keyboardType = UIKeyboardType.NumberPad

    nickelNum.delegate = self
    nickelNum.keyboardType = UIKeyboardType.NumberPad

    pennyNum.delegate = self
    pennyNum.keyboardType = UIKeyboardType.NumberPad
}
@IBAction func userTappedBackground(sender: AnyObject) {
    view.endEditing(true)
}
@IBAction func calculate(sender: UIButton) {
    quarters = Int(quarterNum.text!)!
    dimes = Int(dimeNum.text!)!
    nickels = Int(nickelNum.text!)!
    pennies = Int(pennyNum.text!)!

    var quarterCount = Int(quarters * 25)

    //all dimes * 10 gives the amount of dimes in penny value( 10 dimes = 100 pennies)
    var dimeCount = Int(dimes * 10)


    //all nickels * 5 gives the user the nickels in penny value(5 nickels = 25 pennies)
    var nickelCount = Int(nickels * 5)

    //all the pennies * 1 gives the cents value in pennies(10 pennies = 10 pennies)
    var pennyCount = Int(pennies * 1)

    //Adding all the values together to get the total amount of pennies
    //totalPennies = all the values above added together
    var totalPennies = Int(pennyCount + nickelCount + dimeCount + quarterCount)

    var dollarAmount = totalPennies / 100
    totalPennies %= 100;

    var quarterDollars = totalPennies / 25
    totalPennies %= 25;

    var dimeDollars = totalPennies / 10
    totalPennies %= 10

    var nickelDollars = totalPennies / 5
    totalPennies %= 5

    var pennyDollars = totalPennies / 1
    totalPennies %= 1

    resultOutputLabel.text = String("There is/are ",  String(dollarAmount) , " dollar(s), " , String(quarterDollars) , " quarter(s), " , String(dimeDollars) , " dime(s), " , String(nickelDollars) , " nickel(s), " , String(pennyDollars) , " penny/ies. ")

  }
}

这就是我的代码。构建成功并且所有内容都编译,在我尝试输入数字后它只会出现问题。

1 个答案:

答案 0 :(得分:0)

这些计算很可能是导致崩溃的原因,具体取决于您在文本字段中输入的内容。

quarters = Int(quarterNum.text!)!
dimes = Int(dimeNum.text!)!
nickels = Int(nickelNum.text!)!
pennies = Int(pennyNum.text!)!

一般来说,

var x : Int = Int("1") // returns an optional

因此,你这样做了:

var x : Int = Int("1")!

除此之外,如果传递给Int()的值无法转换为Int,则返回的值为nil。这将导致此时或使用x时发生崩溃。

虽然它增加了复杂性,但您需要准备好处理字符串不可转换的情况。

var x = Int("blah")
if let x = x {
    // conversion to Int success
} else {
    // unable to convert
}