我有两个列表用户界面,两个列表中的单元格相同。对于这两个UI,将管理不同的Codable
数组。即
FirstViewController
包含 FirstDataModel ,而{{11}}包含 SecondDataModel 。
因此,在SecondViewController
的{{1}}方法中,我调用了代码:
FirstViewController
数组:
cellForRow
我想为 FirstDataModel 和 SecondDataModel 设置通用功能。那么如何使用通用函数对其进行管理?
如何在func setPastCell(cell:WeekCell, data_dic:FirstDataModel) {
cell.lblGoalA.text = predictScore1
cell.lblGoalB.text = predictScore2
}
中传递struct FirstDataModel: Codable {
var team1_name:String?
var team2_name:String?
var status:Int?
var image:String?
var score:Int?
}
struct SecondDataModel: Codable {
var team1_name:String?
var team2_name:String?
var status:Int?
var image:String?
var count:Int?
var balance:Int?
}
或FirstDataModel
?
谢谢!
答案 0 :(得分:2)
您可以从协议继承两个DataModel,并在设置中使用协议属性。
示例:
protocol ModelProtocol {
var descriptionOne: String { get }
var descriptionTwo: String { get }
}
struct FirstDataModel: ModelProtocol {}
func setPastCell(cell:WeekCell, data_dic: ModelProtocol) {
cell.lblGoalA.text = descriptionOne
cell.lblGoalB.text = descriptionTwo
}
答案 1 :(得分:1)
创建具有共同属性的协议
<style>
svg{
border: 5px red solid;
height:600px;
width:600px;
}
</style>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 600 600" >
<rect x="295" y="430" width="10" height="100" style="stroke:none; fill: hsl(90, 100%, 47%);"/>
<circle cx="300" cy="430" r="1" stroke="black" stroke-width="2" fill="yellow">
<animate
attributeName="r"
from="1"
to="20"
begin="0s"
dur="2s"
repeatCount="indefinite"
fill="freeze"
/>
</circle>
<rect x="0" y="500" width="600" height="100" style="stroke:none; fill:hsl(120, 100%, 30%);"/>
</svg>
在您的结构中确认协议并添加其属性
protocol DataModel {
var team1_name:String? { get }
var team2_name:String? { get }
var status:Int? { get }
var image:String? { get }
}
当两个结构都确认使用DataModel协议时,可以在function参数中使用它。
struct FirstDataModel: DataModel, Codable {
//Protocol properties
var team1_name:String?
var team2_name:String?
var status:Int?
var image:String?
//additional properties
var score:Int?
}
struct SecondDataModel: DataModel, Codable {
//Protocol properties
var team1_name:String?
var team2_name:String?
var status:Int?
var image:String?
//additional properties
var count:Int?
var balance:Int?
}