# Public viewModel issues

**URL:** <https://forums.kodeco.com/t/public-viewmodel-issues/63771>\
**Category:** RxSwift\
**Created:** [March 6, 2019, 6:53am UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771 "2019-03-06T06:53:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariiasaveleva](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mariiasaveleva/32/83406_2.png) [@mariiasaveleva](https://forums.kodeco.com/u/mariiasaveleva)\
**Post date:** [March 6, 2019, 6:53am UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/1 "2019-03-06T06:53:19Z")

</div>

Hello.

I have a question about implementation of BindableType protocol.  
In book, it looks like this:

```
protocol BindableType {
  associatedtype ViewModelType

  var viewModel: ViewModelType! { get set }

  func bindViewModel()
}

```

So, ViewController which conforms to this protocol will have public var viewModel: TasksViewModel! property.  
I’m not sure if it is a good solution to make public var as a viewModel.

Why is viewModel binding made like this and not with UIViewController’s custom init method with viewModel as a parameter, for example?  
Is it the best possible approach to bind viewModel with UIViewController for MVVM architecture in Swift?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [March 12, 2019, 1:00am UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/2 "2019-03-12T01:00:14Z")

</div>

@fpillet Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![fpillet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fpillet/32/97677_2.png) [@fpillet](https://forums.kodeco.com/u/fpillet)\
**Post date:** [March 12, 2019, 7:32am UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/3 "2019-03-12T07:32:52Z")

</div>

I agree that this is not an ideal solution, as I don’t like force-unwrapping types. This is however what I found to work best in all situations, including when you reanimate view controllers from a storyboard (in this case you don’t have access to the constructor).

This is not cast in stone! You may want to implement a different version of the protocol if you always call the UIViewController initializer. In this case you could pass the VM to the init method.

Feel free to use this chapter more as a guide on techniques, and adapt it to suit your needs.

Hope this helps,  
Florent

---

<div class="post-metadata">

**Author:** ![g\_enius](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/g_enius/32/81494_2.png) [@g\_enius](https://forums.kodeco.com/u/g_enius)\
**Post date:** [July 27, 2019, 12:21pm UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/4 "2019-07-27T12:21:25Z")

</div>

Hi @fpillet,

Can you explain why there must be a force-unwrapping for this associatedtype viewModel?  
I tried to remove it, then I got: Compile error from “Scene+ViewController extension” as below:

Cannot convert value of type ‘TasksViewModel’ to expected argument type ‘TasksViewController.ViewModelType’

Additionally, in that file, if I changed “var” to “let” in below codes, the compiler also complained, “Argument passed to call that takes no arguments”. (I am guessing it’s related to “bindViewModel(to:)” is “mutating”, but I don’t know why)

let vc = nc.viewControllers.first as! TasksViewController  
vc.bindViewModel(to: viewModel)  
return vc

These two questions are killing me, I googled a lot and even re-read swift official docs but couldn’t find the answer. Could you please give me some idea?

Thank you in advance! Much appreciated.

Charles

---

<div class="post-metadata">

**Author:** ![g\_enius](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/g_enius/32/81494_2.png) [@g\_enius](https://forums.kodeco.com/u/g_enius)\
**Post date:** [July 27, 2019, 12:45pm UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/5 "2019-07-27T12:45:52Z")

</div>

I finally figured out the first question myself.

That ! in the protocol is due to “TaskViewControler” and “EditTaskViewController” also define the viewModel as a var !. That’s a “have-to-do” as there are no inits there.

But I am still confusing the second question. Why must be a var to call bindViewMode(to:)?

Cheers,  
Charles

---

<div class="post-metadata">

**Author:** ![g\_enius](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/g_enius/32/81494_2.png) [@g\_enius](https://forums.kodeco.com/u/g_enius)\
**Post date:** [July 27, 2019, 12:54pm UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/6 "2019-07-27T12:54:45Z")

</div>

Oh, I figured out the second question as well. 🙂

As (self.)viewModel is mutated in bindViewModel(to:), so self (a ViewController) must be a var.

Sorry about the noise, I just got confused by compiler’s confusing error messages which didn’t lead me to the right path.☹

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [August 19, 2019, 10:53pm UTC](https://forums.kodeco.com/t/public-viewmodel-issues/63771/7 "2019-08-19T22:53:19Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
