[英]swift struct as function parameter
I have a struct/model that I store some JSON data to fill my tableview: 我有一个结构/模型,我存储了一些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
}
}
Then in my tableview I have a custom cell class that I use when I write out my data: 然后在我的表视图中,我有一个自定义单元格类,可在我写出数据时使用:
let data = MyData[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! CustomTableViewCell
cell.configureCellWithData(data)
What I want to do is to pass the struct as a parameter to the configureCellWithData() but I am not sure how to declare it. 我想做的是将结构作为参数传递给configureCellWithData(),但是我不确定如何声明它。
Instead of doing something like: 而不是做类似的事情:
func configureCellWithData(dataOne: Int, dataTwo: String, dataThree: Int) {
}
then 然后
configureCellWithData(data.valOne,data.valTwo,data.valThree)
I dont want to have many parameters instead I want to send the struct right away 我不想有很多参数,而是我想立即发送该结构
Try this. 尝试这个。
func configureCellWith(data: MyData) {
// Do stuff here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.