这是错误发生的地方,在 let selectedStudent 行,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if(segue.identifier == "Student_segue") {
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedStudent = studentsSorted[indexPath.row]
let destination = segue.destinationViewController as! StudentInfoTableViewController
destination.selectedStudent = selectedStudent
}
}
}
这里是我声明 studentsSorted 和 studentArray 的地方。
typealias studentInfo = Dictionary<String, AnyObject>
typealias studentArray = [studentInfo]
let students = StudentRosterModel()
var studentsSorted:studentArray = studentArray()
var selectedRow:Int = 0
func updateStudentInfo(updatedStudent: Dictionary<String, AnyObject>) {
// replaced the selected row with the updated key/value dictionary
studentsSorted [selectedRow ] = updatedStudent
// sort the revised student list
studentsSorted.sortInPlace{ ($0["last_name"] as? String) < ($1["last_name"] as? String )}
// reload () tableView to show refreshed view
tableView.reloadData()
}
这是我声明 selectedStudent 的地方,
class StudentInfoTableViewController: UITableViewController, UITextFieldDelegate {
var selectedStudent: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
var delegate: studentUpdate?
这里真的很困惑,如果有人能帮助我,我将不胜感激。
最佳答案
Thread 1:EXC_BAD_INSTRUCTION
此错误几乎将错误打印到控制台日志中。我知道这个错误可能是由超出范围的错误引起的。
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedStudent = studentsSorted[indexPath.row]
let destination = segue.destinationViewController as! StudentInfoTableViewController
destination.selectedStudent = selectedStudent
}
如果您将 self.tableView.indexPathForSelectedRow 清除为 indexPath 并且它成功了,那么您认为 indexPath.row 在 studentsSorted 处超过或低于的大小。
关于swift - 线程 1 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP error.。不太确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39950852/