0

每当我运行此代码时,都会调用 VCModel 的 init(),但 Swinject 不会将 VCModel 实例注入到我的 ViewController 中。有人可以告诉我我做错了什么吗?我得到的错误是:

在 ViewController viewModel.cellModels 中展开可选值时意外发现 nil

应用委托

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
}

视图控制器

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

2 回答 2

0

您可以在 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
    }
于 2019-12-23T12:27:00.293 回答
0

您的AppDelegate→ application:didFinishLaunchingWithOptions方法中的代码似乎可以正常工作。我已经用以下代码验证了它:

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

我不知道你VCModelinit方法是什么样的,但看着

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

从您得到的错误中: Unexpectedly Found nil while unwrapping an optional value in ViewController viewModel.cellModels

看起来cellModels我假设它是隐式展开的属性,你必须初始化它VCModelinit方法。

于 2019-12-25T06:51:59.870 回答