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
}
}
在最後部分時,收到錯誤是:夫特,線程1:EXC_BAD_INSTRUCTION錯誤
線程1:exc_bad_instruction(代碼= exc_i386_invop,子碼=爲0x0)錯誤
到targetMoneyLabel.text = targetMoneyString
部分。
我想我必須改變最後一句話,我嘗試閱讀並觀看許多視頻來修復它,但我不能。
與nil
有關的問題?我是Swift新手。
你複查你的網點連接是否正確? –
在我的操場你的代碼是正常工作,我沒有得到錯誤 –
如果targetMoneyLabel!= {零 targetMoneyLabel.text = targetMoneyString } 使用此代碼,而不是最後一行,如果應用程序沒有崩潰,則必須再次重新連接插座 –