gpt4 book ai didi

ios - 不断出现此错误 : Thread 1: EXC_BAD_INSTRUCTION(code=EXC_l386_INVOP, 子代码=0x0)

转载 作者:行者123 更新时间:2023-11-30 13:01:35 25 4
gpt4 key购买 nike

我经常收到此错误,我真的不知道为什么,因为该应用程序相当简单,而且我根据 YouTube 上的教程构建了它,其中一切似乎都工作正常。 (本教程是用 Swift 2 编写的,但是我已经在使用 Swift 3)有人可以帮助我吗?

<小时/>
import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {


@IBOutlet var image1: UIImageView!

@IBOutlet var image2: UIImageView!

@IBOutlet var label1: UILabel!

@IBOutlet var label2: UILabel!

@IBOutlet var message: UILabel!

var data = [["USA", "Italy", "China", "England"], ["Beijing", "London", "Rome", "Washington, DC"] ]

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

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return data.count // Based on data. No hardcoded number.
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

return data[component].count

// For each component (we have 2 components in our array -> the two elements (Countries = USA, Italy, China, England & Capitals = Beijing, London, Rome, Washington) grab the count for the row.

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return data[component][row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

let item1 = data[0][pickerView.selectedRow(inComponent: 0)]
let item2 = data[1][pickerView.selectedRow(inComponent: 1)]

print(item1)
print(item2)

// Grab whatever is selected in index [0] or index [1] into "selectedRow" contrast each other and compare if country and capital match each other.

// Now we refer to the images so that we don't have to type in the exact name over and over again.

let usa = UIImage(named: "USA.png")
let italy = UIImage(named: "Italy.jpeg")
let china = UIImage(named: "China.jpeg")
let england = UIImage(named: "England.jpeg")

let washington = UIImage(named: "Washington.jpeg")
let rome = UIImage(named: "Rome.jpeg")
let beijing = UIImage(named: "Beijing.jpeg")
let london = UIImage(named: "London.jpeg")

let correctMessage = "The Capital of \(item1) is \(item2)"
let wrongMessage = "Match the flags to the Capitals."


switch item1 {

case "USA":

image1.image = usa
label1.text = "USA"

case "Italy":

image1.image = italy
label1.text = "Italy"

case "China":

image1.image = china
label1.text = "China"

case "England":

image1.image = england
label1.text = "England"

default:
image1.image = usa
label1.text = "USA"

} // End Of Switch 1

switch item2 {

case "Beijing":

image2.image = beijing
label2.text = "Beijing"

if label1.text == "China" {

message.text = correctMessage

}

else {

message.text = wrongMessage

}

case "London":

image2.image = london
label2.text = "London"

if label1.text == "England" {

message.text = correctMessage

}

else {

message.text = wrongMessage

}

case "Rome":

image2.image = rome
label2.text = "Rome"

if label1.text == "Italy" {

message.text = correctMessage

}

else {

message.text = wrongMessage

}

case "Washington, DC":

image2.image = washington
label2.text = "Washington, DC"

if label1.text == "USA" {

message.text = correctMessage

}

else {

message.text = wrongMessage

}

default:
image2.image = washington
label2.text = "Washington, DC"

} // End Of Switch 2




} // End Of Did Select Row

} // End Of ViewController | End Of App

最佳答案

我猜测这次崩溃是由于您使用隐式解包选项(顶部的那些 ! 变量)造成的。这些变量允许为 nil,但如果在 nil 时访问,则会导致崩溃。

首先,允许应用程序在调试器中停止,以便您可以查看哪一行负责。然后,验证 Interface Builder 中的所有内容是否正确连接。我的猜测是这些 @IBOutlet 变量中的一个或多个未正确连接。

关于ios - 不断出现此错误 : Thread 1: EXC_BAD_INSTRUCTION(code=EXC_l386_INVOP, 子代码=0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com