Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection in View Controller

I am trying to use dependency injection instead of following singletons. This is how I am trying to achieve. When I run the application I am having an error on "No "decodeObject" candidates produce the expected contextual result type "ModelManager" on that. Any idea how can I implement dependency injection in a right way?

My Model class:

class ModelManager {
var results: MyCustomClass

init(results: MyCustomClass) {
    self.results = results
}
func update(customDataInfo: MyCustomClass!) {
    self.results = customDataInfo
}
}

MyViewController.swift

class MyViewController: UIViewController {

private let modelManager: ModelManager

init(modelManager: ModelManager) {
    self.modelManager = modelManager
    super.init(nibName: nil, bundle: nil)
    self.modelManager.modelManagerUpdate = self as ModelManagerUpdate
}

 required init?(coder aDecoder: NSCoder) {
    self. modelManager = aDecoder.decodeObject(value(forKey: "modelManager") as ModelManager)
    super.init(coder: aDecoder)

    fatalError("init(coder:) has not been implemented")
}

override func encode(with aCoder: NSCoder) {
    super.encode(with: aCoder)
    aCoder.encode(self. modelManager, forKey: "modelManager")
}
}
like image 437
dicle Avatar asked Mar 07 '18 08:03

dicle


People also ask

What is dependency injection in .NET MVC?

The Dependency Injection pattern is a particular implementation of Inversion of Control. Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).

What are the three types of dependency injection?

There are three main styles of dependency injection, according to Fowler: Constructor Injection (also known as Type 3), Setter Injection (also known as Type 2), and Interface Injection (also known as Type 1).

What is dependency injection in Swift stack overflow?

Dependency Injection is a software design pattern in which an object receives other instances that it depends on. It's a commonly used technique that allows reusing code, insert mocked data, and simplify testing. An example could be initializing a view with the network provider as a dependency.


1 Answers

The fact that the init?(coder:) initialiser is being used suggests that your view controller is initialised from storyboard. If that is the case, storyboard does not contain ModelManager, thus it cannot decode it.

You can work around this by wrapping storyboard initialisation into your own method, e.g.:

class MyViewController: UIViewController {
    private var modelManager: ModelManager

    static func create(modelManager: ModelManager) -> MyViewController {
        let vc = /* instantiate vc from storyboard */
        vc.modelManager = modelManager
        return vc
    }
}

If the above method does not suit your needs, I would suggest you take a look at the SwinjectStoryboard framework. It provides - besides basic DI functionality - ability to inject dependencies to the view controllers initialised from storyboard.

like image 172
Jakub Vano Avatar answered Sep 26 '22 21:09

Jakub Vano