Search code examples
iosswiftviewreusability

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


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?'


Solution

  • 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