ios - swift ,线程 1 : EXC_BAD_INSTRUCTION error

标签 ios objective-c swift

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var currencySegment: UISegmentedControl!

@IBOutlet weak var sourceMoneyField: UITextField!

@IBOutlet weak var targetMoneyLabel: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func convertMoney(_ sender: Any) {
    let ratio : Double
    switch currencySegment.selectedSegmentIndex {
    case 0:
        ratio = 0.00085
    case 1:
        ratio = 1178.5
    default :
        ratio = 1.0
    }
    let targetMoneyString: String
    if let sourceMoney = Double(sourceMoneyField.text!){
        targetMoneyString = "\(sourceMoney * ratio)"
    }else {
        targetMoneyString = "Error"
    }

    targetMoneyLabel.text = targetMoneyString
}
}

在最后一部分,我得到的错误是:

thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0) error

targetMoneyLabel.text = targetMoneyString 部分。

我想我必须更改最后一句话,我尝试阅读和观看许多视频来修复它,但我做不到。

问题是否与nil有关?我是 Swift 的新手。

最佳答案

问题一定是关于展开变量。

你可以试试这个:

guard let targetMoneyString.characters.count > 0 else  {
    return
}
targetMoneyLabel.text = targetMoneyString

关于ios - swift ,线程 1 : EXC_BAD_INSTRUCTION error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40909088/

相关文章:

objective-c - Obj-C 文字 : Getting "Array subscript is not an integer" on an NSDictionary query

ios - 日期类型的dateFormatter字符串2013-03-24T02:15:23-08:00 + objective-c

ios - 提供对基类中定义的私有(private)方法的访问

ios - 在 UITextView 中输入时更新 UILabel

objective-c - Shell 脚本调用错误

ios - 超出范围,线程问题

objective-c - UIView 动画在 View 顶部使用轴翻转

ios - Swift json 删除键

swift - 在 Swift 中创建数组集

ios - 在 Firestore 中保存 CLLocationCooridnate2D 数组