Xcode Swift将两个整数变量相乘并在标签文本中存储值

时间:2015-09-08 18:39:47

标签: xcode swift variables int multiplication

我正在使用Xcode 6.4和Swift 1进行项目。我知道当我转到Swift 2时,其中一些会发生变化。我有一些字符串变量是从存储在NSUserDefaults中的值分配的。然后我将它们转换为整数并将它们添加到一起以分配给标签的文本:

self.redKidTotalO.text = "\(sundayRedIncoming.toInt()!+mondayRedIncoming.toInt()!)"
    //Convert label text to integer for math

之后,我取标签文本值并转换为一个整数,用于两个整数变量的乘法运算:

    redIntStarTotal = (self.redKidTotalO.text)!.toInt()

然后我想从NSUserDefaults获取另一个值并将其存储到一个整数变量中并将两者相乘并设置一个新的标签文本:

var paymentIncoming: String!
var paymentIncomingInt: Int!
var paymentMultiply:Int!
var paymentMathTotalConvert: String!
var redIntStarTotal: Int!

if let KEYpaymentMoneyAmountIsNotNill = amountDefaults.objectForKey("KEYpaymentMoneyAmount") as? String {
    paymentIncoming = (amountDefaults.objectForKey("KEYpaymentMoneyAmount") as! String)

    //Take String 'paymentIncoming' and convert to Integer for math
    paymentIncomingInt = paymentIncoming.toInt()
    println(paymentIncomingInt)

    //Do math
    paymentMultiply = paymentIncomingInt * redIntStarTotal
    println("Multiply: \(paymentMultiply)")

    //Take integer 'paymentMultiply' and convert to String for label text
    paymentMathTotalConvert = toString(paymentMultiply)

    // Load payment amount into labels
    self.redPaymentO.text = "(\(paymentMathTotalConvert) cents)"
}

测试这个并在'paymentIncomingInt'上执行println()我得到了这一点。然后我输入乘法代码,它给出了错误:

致命错误:在展开Optional值时意外发现nil (LLDB)

我做错了什么?如何将两个变量整数相乘并将该值显示到标签文本中?谢谢!

0 个答案:

没有答案