【问题标题】:swift struct as function parameter快速结构作为函数参数
【发布时间】:2016-10-20 02:41:34
【问题描述】:

我有一个结构/模型,我存储一些 JSON 数据来填充我的 tableview:

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 configureCellWithData(data: MyData) { }
  • let data = MyData[indexPath.row] 行没有意义。我假设您定义了一些属性,例如 var dataObjects = [MyData]() 或类似的东西,然后您将执行 let data = dataObjects[indexPath.row]

标签: ios swift xcode struct parameters


【解决方案1】:

试试这个。

func configureCellWith(data: MyData) {
    // Do stuff here
}

【讨论】:

  • 是的,或者如果是 Swift 3,也许是 func configureCell(with data: MyData) { ... }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多