简体   繁体   English

这是为xcode 6.2编写的本书中的编码和示例示例的新增功能,而我拥​​有最新版本。

[英]New to coding and working an example in the book which was written for xcode 6.2 and I have the newest version.

I get the following error when I run the app and try and click any key on the keypad that opens on launch. 运行应用程序并尝试单击启动时打开的键盘上的任何键时,出现以下错误。 Here is the error I get in the attached photo 这是所附照片中出现的错误 错误图片

Below is my code and it highlights the first func at the bottom called func formatAsCurrancy... as the problem area. 下面是我的代码,它在底部突出显示了第一个函数,称为func formatAsCurrancy ...作为问题区域。 I have converted all the old code in the book as it prompted me to. 我已经按照书中的提示转换了本书中所有的旧代码。 Not sure what to do at this point. 不知道此时该怎么办。 Thank you for any help. 感谢您的任何帮助。

import UIKit

class ViewController: UIViewController {
    //properties for programmatically interacting with UI components
    @IBOutlet weak var billAmountLabel: UILabel!
    @IBOutlet weak var customTipPercentLabel1: UILabel!
    @IBOutlet weak var customTipPercentageSlider: UISlider!
    @IBOutlet weak var customTipPercentLabel2: UILabel!
    @IBOutlet weak var tip15Label: UILabel!
    @IBOutlet weak var total15Label: UILabel!
    @IBOutlet weak var tipCustomLabel: UILabel!
    @IBOutlet weak var totalCustomLabel: UILabel!
    @IBOutlet weak var inputTextField: UITextField!

    // NSDecimalNumber constants used in the calculateTip method
    let decimal100 = NSDecimalNumber(string: "100.0")
    let decimal15Percent = NSDecimalNumber(string: "0.15")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // select inputTextField so keypad displays when the view loads 
        inputTextField.becomeFirstResponder()
    }

    @IBAction func calculateTip(_ sender: Any) {
        let inputString = inputTextField.text //get user input

        // convert slider value to an NSDecimalNumber
        let sliderValue =
            NSDecimalNumber(integerLiteral: Int(customTipPercentageSlider.value))

        // divide sliderValue by decimal100 (100.0) to get tip %
        let customPercent = sliderValue / decimal100

        // did customTipPercentageSlider generate the event?
        if sender is UISlider {
            // thumb moved so update the Labels with new custom percent
            customTipPercentLabel1.text =
                NumberFormatter.localizedString(from: customPercent,
                                                number: NumberFormatter.Style.percent)
            customTipPercentLabel2.text = customTipPercentLabel1.text
        }

        // if there is a bill amount, calculate tips and totals
        if !(inputString?.isEmpty)! {
            // convert to NSDecimalNumber and insert decimal point
            let billAmount =
                NSDecimalNumber(string: inputString) / decimal100

            // did inputTextField generate the event?
            if sender is UITextField {
                billAmountLabel.text = " " + formatAsCurrency(number: billAmount)

                // calculate and display the 15% tip and total
                let fifteenTip = billAmount * decimal15Percent
                tip15Label.text = formatAsCurrency(number: fifteenTip)
                total15Label.text =
                    formatAsCurrency(number: billAmount + fifteenTip)
                }

            // calculate custom tip and display custom tip and total
            let customTip = billAmount * customPercent
            tipCustomLabel.text = formatAsCurrency(number: customTip)
            totalCustomLabel.text =
                formatAsCurrency(number: billAmount + customTip)
        }
        else {// clear all Labels
            billAmountLabel.text = ""
            tip15Label.text = ""
            total15Label.text = ""
            tipCustomLabel.text = ""
            totalCustomLabel.text = ""
        }
    }

}

// convert a numeric value to localized currency string 
func formatAsCurrency(number: NSNumber) -> String {
    return NumberFormatter.localizedString(
        from: number, number: NumberFormatter.Style.currency)
}
// overloaded + operator to add NSDecimalNumbers
func +(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber {
    return left.adding(right)
}
// overloaded * operator to multiply NSDecimalNumbers
func *(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber {
    return left.multiplying(by: right)
}
// overloaded / operator to divide NSDecimalNumbers
func /(left: NSDecimalNumber, right: NSDecimalNumber) -> NSDecimalNumber {
    return left.dividing(by: right)
}

The simulator is trying to find a numeric keyboard on your computer, but your computer shouldn't have one so that's why it's giving you the error. 模拟器试图在您的计算机上找到数字键盘,但是您的计算机上应该没有数字键盘,因此这就是给您错误的原因。

If you go to the simulator and then Hardware -> Keyboard -> Uncheck use hardware keyboard, the issue will go away. 如果您转到模拟器,然后转到“硬件”->“键盘”->“取消选中使用硬件键盘”,则问题将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM