Search code examples
iosswift4xcode9.1

output showing nil result in console when passing


I am new iOS development and still learning basics, my query is when i passed the data to other view controller from my main tableView controller so it is passing smoothly but the output result is showing nil in the console as shown below here as you can see output in console is nil.This is my second view controller named as movieDetailViewController

below is my main tableView controller from where i am calling my second view controller.

   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
    }
  }
}

Here the structure Movie is a normal swift file containing attributes that i want to print shown as below:

import Foundation

struct Movie {

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

}

Now as i describe that output when passed to another view controller is showing nil. I have been stuck in this problem from 1 week and also searched a lot but no solution helped me in this .


Solution

  • Replace this code in the movieTable View Controller 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
                }
        }
    }
    

    Add an initialiser to your struct

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

    Replace your viewDidLoad code in movieDetailsViewController to

    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!)
    
            }
    
    
        }
    

    Hope this helps. Happy Coding.