我经常遇到这个错误,我真的不知道为什么因为应用程序相当简单,我在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
答案 0 :(得分:0)
我猜这次崩溃来自你使用隐式解包的选项,顶部是那些!
变量。这些变量允许为nil
,但如果在nil
时访问则会导致崩溃。
首先,允许应用程序在调试器中停止,这样您就可以看到哪一行负责了。然后,验证您是否已在Interface Builder中正确连接所有内容。我的猜测是这些@IBOutlet
变量中的一个或多个没有正确连接。