ios - 无法在属性初始值设定项中使用实例成员 'parseData'

标签 ios swift

我从下面的代码中得到这个错误。我已经阅读并尝试了一些类似问题的解决方案,但它们并没有奏效。任何帮助将不胜感激。

class allTripsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let list = parseData()

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return list.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    cell.textLabel?.text = list[indexPath.row]
    return cell
}

func parseData() -> [String] {
    //get some json data and put it into the array
    return delays
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

如果 let list = ["Milk", "Honey"] 等,这很好用。完美地呈现在 table 上。但我需要它来使用 parseData 中的数组。

最佳答案

您是否尝试过将列表数组分配为空数组,然后用解析后的值填充它?

var list = [String]()

override func viewDidLoad(){
super.viewDidLoad()

list = parseData()

tableView.reloadData()

}

从主队列中的 parseData 获取值也很重要。如果您异步获取 parseData,则必须在 parseData 中使用完成 block 来返回主队列中的值。 如果您需要更多详细信息,请告诉我

关于ios - 无法在属性初始值设定项中使用实例成员 'parseData',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461650/

相关文章:

ios - 为静态 UITableViewCell 子类创建属性

swift - AVAudioSession : microphone headphone as input and iphone speaker as output

swift - 在实现协议(protocol)的结构上调用方法

swift - 如何使用 SwiftUI 以编程方式转换 View ?

iphone - 带有 "+"的电话号码与没有 "+"的电话号码不同吗?

ios - 尽管有启动屏幕文件,为什么还需要启动图像?

ios - Storyboard中的 UITableView 隐藏第一个单元格时,它会好吗?为什么?

ios - 所有 iPhone 模拟器的 ScrollView

swift - 将显式展开的枚举类型的属性与常量进行比较

点击另一个 TextField 时,SwiftUI TextField 不会提交更改