无法将类型为'()'的值分配为类型为'UIView?'

问题描述 投票:0回答:1

我创建了一个视图以供重用。我的计划是这样的:在ViewController上显示相同的内容,具体取决于类型。在视图上,您​​需要更改Label和背景。数据来自服务器,为此,我创建了ViewModel。根据类型,我返回Label的内容和背景颜色。

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

错误:无法将类型'()'的值分配为类型'UIView?'

ios swift view reusability
1个回答
1
投票

我不知道您在做什么,将您的代码更正后才能使用。它应该看起来像这样:

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 {

}

但是我仍然不知道您想为viewOne : UIView = getType(ViewType)分配什么来返回一个Int

© www.soinside.com 2019 - 2024. All rights reserved.