EXC_BAD_INSTRUCTION endpoint when running xCode application

I have a table view in my application, and when I run my application, it crashes on the following function.

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell
}

He crashes on line var cell

It gives the following error: error

I can not understand what is wrong with my code.

The whole function:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell

    let data: NSManagedObject = mylist[ip.row] as NSManagedObject

    cell.textLabel.text = data.valueForKeyPath("voornaam") as String
    cell.detailTextLabel.text = data.valueForKeyPath("achternaam") as String
    return cell
}

EDIT : What I got now: (still giving the same error)

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell? = tableView?.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell

    if cell == nil {
        cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
    }

    let data: NSManagedObject = mylist[indexPath.row] as NSManagedObject

    cell!.textLabel.text = data.valueForKey("voornaam") as String
    cell!.detailTextLabel.text = data.valueForKey("achternaam") as String
    //cell!.textLabel.text = "Hoi"
    return cell
}
+4
source share
4 answers

, as , , . dequeue nil . as?, , , :

var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell

if cell == nil {
  cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
}

...

cell , cell!, , UITableViewCell .

, : . dequeue . UITableViewCell , cellFor.... dequeue.

+1

-, if let ip = indexPath? , . .

let data , .

0

, ? .

0

It may be too late, but I like to share my experience. I had a similar error as I copied all the code from another project. Therefore, I think that the variables and functions will not be recognized, so I had to drag them (cntr + drag), after which it will be solved. Sorry if I can’t explain better. I am new to this.

0
source

Source: https://habr.com/ru/post/1548207/


All Articles