如何在协议中正确使用associatedType [英] How can I correctly use associatedType in my protocol

查看:372
本文介绍了如何在协议中正确使用associatedType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的tableviewcell提供一个面向协议的MVVM.我有很多.

I’m trying to come up with an protocol-oriented MVVM for my tableviewcells. I have lots of them.

我的viewModel

protocol PlainTableViewCellModelType {
    var backgroundColor : UIColor {get}
    var textColor: UIColor {get}
    var titleFont : UIFont {get }
    var accessoryType : UITableViewCellAccessoryType {get}
    var textLabelNumberOfLines: Int {get}
}

我的view

protocol PlainTableViewCellType{
    associatedtype viewModel : PlainTableViewCellModelType
    func setupUI(forViewModel viewModel: viewModel)
}

我的class符合性

class PlainTableViewCell : CCTableViewCell, PlainTableViewCellType{

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }

    func setupUI(forViewModel viewModel: PlainTableViewCellModelType){
        contentView.backgroundColor = viewModel.backgroundColor
        textLabel?.textColor = viewModel.textColor
        textLabel?.font = viewModel.titleFont
        accessoryType = viewModel.accessoryType
        textLabel?.numberOfLines = viewModel.textLabelNumberOfLines
    }
}

当前设置导致以下错误:

The current setups results in the following error:

类型'PlainTableViewCell'不符合协议 'PlainTableViewCellType'

Type 'PlainTableViewCell' does not conform to protocol 'PlainTableViewCellType'

如果可以,我可以使它工作:

I can get it to work if I do:

protocol PlainTableViewCellType{
    func setupUI(forViewModel viewModel: PlainTableViewCellModelType)
}

但是我想拥有一个associatedType,所以我可以在所有PlainTableViewCellType函数中强制使用相同的模型

But I want to have an associatedType so I can enforce same model in all my PlainTableViewCellType functions

我很高兴听听替代方案,但首先我想知道为什么这行不通.

I'm happy to listen to alternatives, but first I want to know why this doesn't work.

推荐答案

您必须创建PlainTableViewCellModelType实现,并在单元实现中声明typealias.将其用作setupUI(forViewModel:)中参数的类型,因为编译器无法推断关联的类型.

You must create PlainTableViewCellModelType implementation and declare typealias in cell implementation. Use it as type for parameter in setupUI(forViewModel:) because compiler cant infer associated type.

protocol PlainTableViewCellModelType {
    var backgroundColor : UIColor { get }
    var textColor: UIColor { get }
    var titleFont : UIFont { get }
    var accessoryType : Int { get}
    var textLabelNumberOfLines: Int { get }
}

protocol PlainTableViewCellType{
    associatedtype viewModel : PlainTableViewCellModelType
    func setupUI(forViewModel viewModel: viewModel)
}

struct PlainTableViewCellModel: PlainTableViewCellModelType {
    var backgroundColor : UIColor
    var textColor: UIColor
    var titleFont : UIFont
    var accessoryType : Int
    var textLabelNumberOfLines: Int
}


class PlainTableViewCell : UITableViewCell, PlainTableViewCellType {
    typealias viewModel = PlainTableViewCellModel

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }

    func setupUI(forViewModel viewModel: PlainTableViewCellModel) {
        contentView.backgroundColor = viewModel.backgroundColor
        textLabel?.textColor = viewModel.textColor
        textLabel?.font = viewModel.titleFont
        textLabel?.numberOfLines = viewModel.textLabelNumberOfLines
    }
}

这篇关于如何在协议中正确使用associatedType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆