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



我创建了一个供重用的视图。我的计划是这样的:根据类型在ViewController上显示具有不同内容的相同内容。 在查看时,您需要更改标签和背景。数据来自服务器,为此我创建了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
}
}
}

视图控制器视图模型.swift

class ViewControllerViewModel{
var viewType: ViewType? = nil
func getViewType(type: viewType)  {
switch type {
case .oneViewType:
return
case .twoViewType:
return
}
}

视图控制器.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?

我不知道你在做什么,在更正你的代码工作之后。它应该是这样的:

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

但是我仍然不知道您要分配返回 IntviewOne : UIView = getType(ViewType)分配什么

相关内容

最新更新