view持有model的问题
- view与model的耦合
- 有时view需要持有多个model,view中代码较多
view持有一个遵守了某个协议的model
解决的问题:如果view持有model过多,避免view中出现过多model(不过这种情况较少)
代码:
protocol:
protocol ModelProtocol {
var name: String { get }
}
model:
struct FirstModel {
var name1: String?
}
extension FirstModel: ModelProtocol {
var name: String {
return name1 ?? ""
}
}
struct SecondModel {
var name2: String?
}
extension SecondModel: ModelProtocol {
var name: String {
return name2 ?? ""
}
}
view:
var model: ModelProtocol? {
didSet{
titleLabel.text = model?.name
}
}
给view的model赋值:
let model1 = FirstModel(name1: "好好好")
yellowView.model = model1
网友评论