![](/img/trans.png)
[英]How to select a property of a struct dynamically to pass as a parameter in a function in swift 4?
[英]swift struct as function parameter
我有一個結構/模型,我存儲了一些JSON數據來填充表格視圖:
import SwiftyJSON
struct MyData {
let valOne: Int
let valTwo: String
let valThree: Int
init(json: JSON) {
valOne = json["some_data"].intValue
valTwo = json["auc_data"].stringValue
valThree = json["auc_data"].intValue
}
}
然后在我的表視圖中,我有一個自定義單元格類,可在我寫出數據時使用:
let data = MyData[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! CustomTableViewCell
cell.configureCellWithData(data)
我想做的是將結構作為參數傳遞給configureCellWithData(),但是我不確定如何聲明它。
而不是做類似的事情:
func configureCellWithData(dataOne: Int, dataTwo: String, dataThree: Int) {
}
然后
configureCellWithData(data.valOne,data.valTwo,data.valThree)
我不想有很多參數,而是我想立即發送該結構
嘗試這個。
func configureCellWith(data: MyData) {
// Do stuff here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.