通过时在控制台中显示 nil 结果的输出

output showing nil result in console when passing

我是新 iOS 开发人员,仍在学习基础知识,我的查询是当我将数据从我的主 tableView 控制器传递到其他视图控制器时,所以它传递顺利,但输出结果在控制台如下图 here as you can see output in console is nil.This is my second view controller named as movieDetailViewController

下面是我的主 tableView 控制器,我从那里调用我的第二个视图控制器。

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? movieDetailViewController
    {
        destination.film = movies[(tables.indexPathForSelectedRow?.row)!] as? Movie
    }
  }
}

这里的 Movie 结构是一个普通的 swift 文件,其中包含我要打印的属性,如下所示:

import Foundation

struct Movie {

     var title: String
     var overview: String
     var poster_path: String

}

现在正如我所描述的那样,传递给另一个视图控制器的输出显示为 nil。从 1 周开始,我一直陷入这个问题,也进行了很多搜索,但没有解决方案帮助我解决这个问题。

试试这个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetails", let destination = segue.destination as? movieDetailViewController {
        if let cell = sender as? UITableViewCell, let indexPath = tables.indexPath(for: cell) {
            destination.film = movies[indexPath.row] as? Movie
        }
    }
}

试试这个

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


 let mv = movies[indexPath.row]

 performSegue(withIdentifier: "showDetails", sender: mv)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let destination = segue.destination as? movieDetailViewController
{
    destination.film = sender as? Movie
}
}

替换 movieTable 视图控制器中的这段代码 class

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? movieDetailViewController
    {
        if let selectedIndex = tables.indexPathForSelectedRow?.row,
            let selectedTitle = movies[selectedIndex]["title"],
            let selectedOverView = movies[selectedIndex]["overview"],
            let selectedPosterPath : String = movies[selectedIndex]["poster_path"] as! String
            {
                destination.film = Movie.init(title: selectedTitle as! String, overView: selectedOverView as! String, posterPath:selectedPosterPath) as! Movie
            }
    }
}

向您的结构添加初始化程序

init(title t: String, overView: String, posterPath pp: String) {
    title = t
    overview = overView
    poster_path = pp
}

movieDetailsViewController 中的 viewDidLoad 代码替换为

override func viewDidLoad() {
        super.viewDidLoad()
        if let title = film?.title,
           let movieoverview = film?.overview,
            let urlString = film?.poster_path
        {
            movieTitle.text = title
            print(movieTitle.text)

            movieOverview.text = movieoverview

            print(movieOverview.text)

            let urlString = URL(string: ("http://image.tmdb.org/t/p/w185/") + urlString)
            moviePoster.downloadedFrom(url: urlString!)

        }


    }

希望这对您有所帮助。快乐编码。