如何在协议中正确使用associatedType

编程入门 行业动态 更新时间:2024-10-28 02:35:10
本文介绍了如何在协议中正确使用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

本文发布于:2023-11-15 01:50:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   协议   如何在   associatedType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!