ios - 编码新手,正在工作书中的一个示例,该示例是为 xcode 6.2 编写的,我有最新版本。

标签 ios iphone swift xcode

当我运行应用程序并尝试单击启动时打开的键盘上的任意键时,出现以下错误。 这是我在所附照片 photo of error 中遇到的错误

下面是我的代码,它突出显示底部的第一个名为 func formatAsCurrancy... 的函数作为问题区域。我已经按照提示转换了书中的所有旧代码。不知道此时该怎么办。感谢您的任何帮助。

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)
}

最佳答案

模拟器正在尝试在您的计算机上查找数字键盘,但您的计算机不应该有数字键盘,因此它会向您显示错误。

如果您进入模拟器,然后选择硬件 -> 键盘 -> 取消选中使用硬件键盘,问题就会消失。

关于ios - 编码新手,正在工作书中的一个示例,该示例是为 xcode 6.2 编写的,我有最新版本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42036021/

相关文章:

ios - 使用 NSLayoutConstraints 使用 Swift 在 ScrollView 中垂直对齐动态 View

ios - 如何将 UIView 放入 UICollectionView 中?

ios - 在没有 iCloud 授权的情况下,有什么方法可以从 iCloud 商店读取数据?

ios - 在 Collection View 中访问 indexPath

ios - 添加带有向上动画的部分/行

ios - 从模型中调用 CLLocationManager 及其委托(delegate)

iphone - 通过 iOS 设备连接时无法从本地服务器获得响应

ios - 如何在 iOS 中通过 Twitter API 获取用户电子邮件地址?

ios - UISlider 不会在将应用程序语言更改为 RTL 时翻转。 | iOS

ios - 在 'self' 调用之前使用的 'self.init' 错误或在不同模块的 init 上分配给 'self'