为 segue 传递数据 nil 做准备

Prepare for segue passing Data nil

我直接改了none的代码,但是更新到Xcode12后,prepare for Segue传数据的方法就失效了

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toDetails" {
        if let indexPath = sender as? IndexPath, let nextVC = segue.destination as? JobDetailViewController {
            nextVC.jobDetails =
                JobDetailViewController.JobDetails(jobDetail: jobs[indexPath.row].text, userName: jobs[indexPath.row].addedByUser!, jobImage: jobs[indexPath.row].jobImage, downloadURL: jobs[indexPath.row].downloadURL!)
        }
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   performSegue(withIdentifier: "toDetails", sender: indexPath.row)

}

详细VC:

var jobDetails: JobDetails?

struct JobDetails {
    var jobDetail: String
    var userName: String
    var jobImage: UIImage?
    var downloadURL: String?
}

(举例) 通过 print("Hello (jobDetails?.userName)") 我得到 "Hello nil" 作为输出

新 Swift/Xcode 版本有什么变化吗? 我也没有收到任何 errors/warnings

请看一下,您正在传递 indexPath.row,它是 Int 并且准备将其解析为 IndexPath。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       performSegue(withIdentifier: "toDetails", sender: indexPath)
    }