gpt4 book ai didi

ios - 扩展中无法识别 Swift 类属性

转载 作者:行者123 更新时间:2023-11-28 11:39:34 24 4
gpt4 key购买 nike

这让我困惑了一段时间。我正在尝试将列表出列到 TableView 中(嵌入在 View Controller 中)。

class mainViewController: UIViewController {
var topicsE1: [Topic] = [
Topic(title: "Practice", startDate: "January 5, 2019"),
Topic(title: "Fundamentals", startDate: "January 6, 2019")]

扩展名:

extension mainViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "topicCellE1", for: indexPath)

cell.textLabel?.text = "\(topicsE1.title)" //Error message is here
return cell
}

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

类在另一个文件中定义

class Topic {
var title: String
var startDate: String

init(title: String, startDate: String) {
self.title = title
self.startDate = startDate
}
}

扩展程序识别了 topicsE1,但给出了错误

"Value of type '[Topic]' has no member 'title'"

当代码如上(topicsE1.title)。

最佳答案

你需要

cell.textLabel?.text = topicsE1[indexPath.row].title  

因为 topicsE1 是一个数组,您不能将 .title 直接附加到它

关于ios - 扩展中无法识别 Swift 类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com