Ios Swift,线程1:EXC\u错误\u指令错误
在最后一部分中,我得到的错误是: 线程1:exc_bad_指令(代码=exc_i386_invop,子代码=0x0)错误 到Ios Swift,线程1:EXC\u错误\u指令错误,ios,objective-c,swift,Ios,Objective C,Swift,在最后一部分中,我得到的错误是: 线程1:exc_bad_指令(代码=exc_i386_invop,子代码=0x0)错误 到targetMoneyLabel.text=targetMoneyString部分 我想我必须改变最后一句话,我试着阅读和观看很多视频来修正它,但我做不到 问题是否与nil有关?我是Swift新手。问题一定是关于展开变量 您可以尝试以下方法: import UIKit class ViewController: UIViewController { @IBOutlet
targetMoneyLabel.text=targetMoneyString
部分
我想我必须改变最后一句话,我试着阅读和观看很多视频来修正它,但我做不到
问题是否与
nil
有关?我是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
}
}
问题一定是关于展开变量 您可以尝试以下方法:
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
}
}
您是否重新检查了您的插座是否正确连接?在我的游乐场中,您的代码工作正常,如果targetMoneyLabel!=nil{targetMoneyLabel.text=targetMoneyString}使用此代码而不是最后一行,如果应用程序没有崩溃,那么您必须重新连接插座在您的错误中,您删除了任何有用的信息。非常感谢,我使用了“if targetMoneyLabel!=nil{targetMoneyLabel.text=targetMoneyString}”,我终于删除了我的错误您是否重新检查了您的插座是否正确连接?在我的游乐场中,您的代码工作正常,如果targetMoneyLabel!=nil{targetMoneyLabel.text=targetMoneyString}使用此代码而不是最后一行,如果应用程序没有崩溃,那么您必须重新连接插座在您的错误中,您删除了任何有用的信息。非常感谢您,我使用了“if targetMoneyLabel!=nil{targetMoneyLabel.text=targetMoneyString}”,我最终删除了我的错误,但它带来了另一个问题,即“条件中的变量绑定需要初始值设定项”。好消息是,无论如何,我修复了过去的错误。非常感谢。你的建议太好了,我会继续尝试的,但它带来了另一个问题,即“条件中的变量绑定需要初始值设定项”。好消息是,无论如何,我修复了过去的错误。非常感谢。你的建议太好了,我会继续尝试的