0

Whenever I run this code the init() of the VCModel gets called but Swinject is not Injecting the VCModel instance to my ViewController. Can Someone Please tell me what am I doing wrong? Error I get is:

Unexpectedly Found nil while unwrapping an optional value in ViewController viewModel.cellModels

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    container = Container() { con in
        con.register(VCModeling.self) { _ in
            VCModel()
        }

        con.storyboardInitCompleted(ViewController.self) { r, c in
            c.viewModel = r.resolve(VCModeling.self)!
        }
    }

    let window = UIWindow(frame: UIScreen.main.bounds)
    window.backgroundColor = UIColor.white
    window.makeKeyAndVisible()
    self.window = window
    let bundle = Bundle(for: ViewController.self)
    let storyboard = SwinjectStoryboard.create(name: "Main", bundle: bundle, container: container)
    window.rootViewController = storyboard.instantiateInitialViewController()

    return true
}

ViewController

private let disposeBag = DisposeBag()
var viewModel: VCModeling!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    viewModel.cellModels
     .bind(to: tableView.rx.items(cellIdentifier: "myCell", cellType: MyCellClass.self)) {
         i, cellModel, cell in
       cell.viewModel = cellModel
     }.disposed(by: disposeBag)
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59

2 Answers2

0

Can you try out the following code in AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        container = Container() { con in
            con.register(VCModeling.self) { _ in
                VCModel()
            }

            con.storyboardInitCompleted(ViewController.self) { r, c in
                c.viewModel = r.resolve(VCModeling.self)!

                let window = UIWindow(frame: UIScreen.main.bounds)
                window.backgroundColor = UIColor.white
                window.makeKeyAndVisible()
                self.window = window
                window.rootViewController = c
            }
        }
        return true
    }
Zeeshan Ahmed
  • 834
  • 8
  • 13
0

Code in your AppDelegate → application:didFinishLaunchingWithOptions method does seems to work properly. I have verified it with following code:

class ViewController: UIViewController {

    var viewModel: VCModeling!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print(viewModel.uuid)
    }
}

protocol VCModeling {
    var uuid: UUID { get }
}

class VCModel: VCModeling {
    let uuid: UUID
    init() {
        self.uuid = UUID()
    }
}

I can not tell what your VCModel's init method looks like but looking at

    ...
// Do any additional setup after loading the view.
    viewModel.cellModels
    ...

From the error you are getting: Unexpectedly Found nil while unwrapping an optional value in ViewController viewModel.cellModels

It looks like cellModels which I assume is implicitly unwrapped property, you must initialize it VCModel's init method.

ahsumra
  • 87
  • 10