Dependency Injection - iOS 13

Hello Devs!! Hello from NY o/
On this particular case, I'm make some studying about Dependency Injection and I'm just making a couple mockups to improve my lessons.

By the way, I would like understand more deep why the ...{ coder in ... at my closure be represented a casting with my another class as something like this below:

...
Code Block
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Movie"
    }
    @IBAction func inject(_ sender: Any) {
        
        let movie = Movie(title: "GoldenEye 007", genre: "Action", poster:  imageLiteral(resourceName: "007 Goldeneye"))
        
        guard let moviesDetailsVC = self.storyboard?.instantiateViewController(identifier: "ShowMore", creator: { coder in
            return MoviewsDetailViewController(coder: coder, movie: movie)
            
        }) else {
            fatalError("MoviesViewController has not been implemented")
        }
        
        self.navigationController?.pushViewController(moviesDetailsVC, animated: true)
    }
    
}

...

That's my another class:

Code Block
import UIKit
class MoviewsDetailViewController: UIViewController {
    
    @IBOutlet weak var posterImageView: UIImageView!
    var movie: Movie
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.title = movie.title
        self.posterImageView.image = movie.poster
    }
    
    init?(coder: NSCoder, movie: Movie) {
        self.movie = movie
        super.init(coder: coder)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

Cheers!!!
You should use Paste and match style to copy code. That will avoid the extra blank lines that make code really hard to read:

Code Block
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Movie"
}
@IBAction func inject(_ sender: Any) {
let movie = Movie(title: "GoldenEye 007", genre: "Action", poster: imageLiteral(resourceName: "007 Goldeneye"))
guard let moviesDetailsVC = self.storyboard?.instantiateViewController(
identifier: "ShowMore",
creator: { coder in
return MoviewsDetailViewController(coder: coder, movie: movie)
}) else {
fatalError("MoviesViewController has not been implemented")
}
self.navigationController?.pushViewController(moviesDetailsVC, animated: true)
}
}


Code Block
import UIKit
class MoviewsDetailViewController: UIViewController {
@IBOutlet weak var posterImageView: UIImageView!
var movie: Movie
override func viewDidLoad() {
super.viewDidLoad()
self.title = movie.title
self.posterImageView.image = movie.poster
}
init?(coder: NSCoder, movie: Movie) {
self.movie = movie
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}


Here is what doc says for creator:

creator
A block containing your custom creation code for the view controller. Use this block to create the view controller, initialize it with the provided coder object and any custom information you require, and return the result. This block returns a new view controller object and takes the following parameter:
coder
The coder object containing the storyboard data to use when configuring the view controller. 
If you return nil from your block, this method creates the view controller using the default init(coder:)method.

What is it you don't understand ?

Hello, Claude31!

I have been read the documentation before diving into this implementation. I was searching for whats exactly the behaviour behind into this.

Besides, you don't understand my question and you just put the Docs and retrieve me another question.

If you want to help, I could to say thanks, really. You should ask before as well to answer generic way.

By the way, thanks for feedback about paste code. But you don't help with your reply.

Cheers.

You're right, I don't understand your question, that's why I asked for details:

I would like understand more deep why the ...{ coder in ... at my closure be represented a casting with my another class as something like this below: 

Is it my understanding of english or your formulation of the question … ? Anyway, I will let others help you if they understand better.

Note: you tagged as Interface Builder. Not sure you are searching in the right direction.

have a good day.
Accepted Answer
Hello, Claude31.

But this implementation that I have been done envolves Storyboard and have a relationship with interface builder. You don't understand again.
Besides, for a test only, without a biggest project and not merge conflicts, what's the problem for storyboards?

Interface Builder can be either XIB/nib or Storyboard. Latter of which is the more recent (and recommended) method provided by Apple.



By the way, I have understand without your help.
Have a nice day for you too.

Cheers.
Good you found the answer ; may be you could have shared the explanation here.

A last point and I will not bother you on your future posts: may I remind you the code of behavior on this forum: we must stay polite and respectful of other developers.

Unfortunately, your post and your comment
"You don't understand again. "

does not follow this rule.

Have a good day.
Hello, Claude31.

I'm not disrespecting the developers. I would never do that.

I just say that you don't understand my question again because you have been say:

"Note: you tagged as Interface Builder. Not sure you are searching in the right direction. have a good day."

I'm using interface builder for a test only...

So sorry for all inconvenience.

Have a nice a day for you too.

Cheers.
Dependency Injection - iOS 13
 
 
Q