swift - 线程 1 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP error.。不太确定为什么

标签 swift xcode

这是错误发生的地方,在 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.rowstudentsSorted 处超过或低于的大小。

关于swift - 线程 1 : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP error.。不太确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39950852/

相关文章:

ios - Xcode 无法识别我的 pod 框架

ios - 从 Parse Cloud 更新 Parse Local Storage 中固定的 PFObject

ios - Swift 从 JSON 获取数组值的结果

ios - 我在 appDelegate 中收到错误?

arrays - 为什么 [SomeStruct] 不能转换为 [Any]?

swift - 将整行附加到二维数组

ios - 运行程序时出现未知错误

ios - Xcode/iOS - 尝试通过简单的打印来进行控制台调试,但无法正常工作。不知道如何找到问题

ios - 使用 Swift 从 Firebase 中删除整个子项

swift - 如何检测是否已触摸 SKSpriteNode