Свойство Swift Class не распознается в расширении - PullRequest
0 голосов
/ 07 января 2019

Это сбивает меня с толку уже некоторое время. Я пытаюсь удалить список из списка в табличное представление (встроенное в контроллер представления).

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
    }
}

Расширение распознает темы E1, но выдает ошибку

"Значение типа '[Topic]' не имеет члена 'title'"

когда код такой, как указано выше (themesE1.title).

1 Ответ

0 голосов
/ 07 января 2019

Вам нужно

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

as topicsE1 - это массив, который нельзя добавить .title непосредственно к нему

...