"Expression type is ambiguous without additional context" when trying to create a new variable to be able to add an array

I am new to coding and Swift and now I am trying to create a small application. I created this piece of code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let cell = tableView.cellForRowAtIndexPath(indexPath)
    var selectedSubject = toBeAddedSubjects[indexPath.row] as Subject
    selectedSubject.name = cell.nameLabel
    selectedSubject.semester = cell.semesterLabel

    if cell?.accessoryType == UITableViewCellAccessoryType.Checkmark {
    cell?.accessoryType = UITableViewCellAccessoryType.None;

    } else {
    cell?.accessoryType = UITableViewCellAccessoryType.Checkmark;
    selectedCellsData.append(newElement: selectedSubject)
    }
    }

      

Now I am getting the error

Expression type is ambiguous without additional context

for cell.nameLabel

and cell.semesterLabel

. They were, however, already used in the previous code snippet:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("AddSubjectCell", forIndexPath: indexPath) as! SubjectCell

    let subject = Subjects[indexPath.row] as Subject
    let Semester = "\(subject.semester)"

    cell.nameLabel.text = subject.name

    cell.semesterLabel.text = "Semester " + Semester

    return cell

}

      

My goal is to add an append function at the end of the first code, so I need to convert the cell information to the correct type for selectedSubject

.

var selectedCellsData = [ Subject(name: "Initial Subject", semester: 0)]

      

+3


source to share


1 answer


It looks like you are producing your cell as SubjectCell

in the second working code example, but not in the problem code. Make sure you add the cell to your own class to access your custom properties.

let cell = tableView.cellForRowAtIndexPath(indexPath) as! SubjectCell

      



In addition, you assign UILabels to properties name

and semester

yours Subject

. Do you want to assign text to these labels?

selectedSubject.name = cell.nameLabel.text
selectedSubject.semester = cell.semesterLabel.text

      

0


source







All Articles