Search code examples
arraysswiftstringuitableviewcell

Cannot call value of non-function type '[String]'


I want to set the text of a label in a prototype cell at a string that is stored in an array.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "unitOptionCell", for: indexPath)

    cell.textLabel?.text = currentConvertion.availableUnits(indexPath)

    return cell
}

The array is the follow:

var availableUnits: [String] {
    switch category {
    case .firstCategory:
        return ["test 1", "test 2", "test 3"]
    case .secondCategory:
        return ["test 4", "test 5", "test 6"]
    case .thirdCategory:
        return ["test 7", "test 8", "test 9"]
    }
}

where is the error?


Solution

  • If you use parenthesis, the compiler interprets that you're trying to make a call to a function. You should use brackets access the elements of your array, something like:

    cell.textLabel?.text = currentConvertion.availableUnits[indexPath.row]