簡體   English   中英

我在 Swinject 中做錯了什么?

[英]What am I Doing wrong in Swinject?

每當我運行此代碼時,都會調用 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)
}

您可以在 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
    }

AppDelegateapplication: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方法對其VCModel init

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM