-1

I created a view for reuse. My plan is this: Show the same thing on ViewController with different content depending on the type. On view, you need to change the Label and background.The data comes from the server, for this I created the ViewModel. Depending on the type, I return the Label content and background color.

ViewModel.swift

  class SomeViewModel{
        enum ViewType: Int {
            case oneViewType
            case twoViewType
        }
        //Example
          func getViewBackgroundColor() -> UIColor {
                switch ViewType {
                case .oneViewType:
                    return .black
                case .twoViewType :
                    return .white
                }
            }
    }

ViewControllerViewModel.swift

class ViewControllerViewModel{

    var viewType: ViewType? = nil

    func getViewType(type: viewType)  {

        switch type {
        case .oneViewType:
            return
        case .twoViewType:
            return
        }
    }

ViewController.swift

 class SomeViewController: UIViewController {

        @IBOutlet weak var oneView:UIView!
        @IBOutlet weak var twoView:UIView!

      var viewModel: SomeViewModel!



    override func viewDidLoad() {
        super.viewDidLoad()

    }
    func configure(viewModel: ViewControllerViewModel) {
        self.viewModel = viewModel

   //ERROR : Cannot assign value of type '()' to type 'UIView?'
    oneView = self.SomeViewModel.getViewType(type: .oneViewType)
        }
}

Error: Cannot assign value of type '()' to type 'UIView?'

jojiReptiloid
  • 286
  • 1
  • 10
  • What exactly is your question? – koen May 21 '20 at 12:15
  • What is the code you showed, is it part of `ViewController`? Please add some more details in your code showing how `ViewModel`, `View` and `ViewController` are set up together. – koen May 21 '20 at 12:32
  • You don't pass data from the model to the view controller. The view controller is using the model. So, when you call the view for display (ho do you get there ? With segue ?), just pass the right ViewType (for instance in prepare). Then in viewDidLoad or viewWillAppear, call the getViewBackgroundColor() and set the background with it. – claude31 May 21 '20 at 12:37
  • @koen I gave details – jojiReptiloid May 21 '20 at 12:39
  • You get the error because you are not returning a `UIView` in `getViewType`. – koen May 22 '20 at 02:18
  • And MVVM is a pretty advanced subject. Maybe try to learn swift with a simpler project? – koen May 22 '20 at 02:21

1 Answers1

1

I don't know what you are doing, after correct your code to works. It should be look like this :

enum ViewType: Int {
    case oneViewType
    case twoViewType

    func getViewBackgroundColor() -> UIColor {
    switch self {
        case ViewType.oneViewType:
            return .black
    case ViewType.twoViewType :
            return .white
        }
    }
}

class SomeViewModel{
    var type : ViewType

    init(type : ViewType) {
        self.type = type
    }
}

class ViewControllerViewModel{

var viewType: ViewType? = nil

func getViewType(type: ViewType)  {

    switch type {
    case .oneViewType:
        return
    case .twoViewType:
        return
    }
}
}

class WalletsViewModel : SomeViewModel {

}

But i still not know what you want to assign viewOne : UIView = getType(ViewType) which return an Int

King.lbt
  • 843
  • 5
  • 15