0

Why does the first line in the method below give me an EXC_BAD_INSTRUCTION runtime error?

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cellIdentifier = NSStringFromClass(MessageCell)
    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MessageCell
    // ...
}
ma11hew28
  • 121,420
  • 116
  • 450
  • 651

2 Answers2

1

Swift does not have the introspection capabilities yet as Obj-C does.

class Cell: UITableViewCell {

}

let a = NSStringFromClass(NSString) // prints NSString
let b = NSStringFromClass(Cell) // prints _TtC11lldb_expr_04Cell
let c = NSStringFromClass(UITableViewCell) // prints UITableViewCell

Since the identifier gets modified you try to dequeue a non-existent cell which leads to the mentioned error.

See also Get a user-readable version of the class name in swift (in objc NSStringFromClass was fine)

Community
  • 1
  • 1
artey
  • 1,243
  • 7
  • 10
  • Good guess. But, I was actually using `cellIdentifier` in both places. Thanks though. I figured it out. – ma11hew28 Jul 02 '14 at 17:16
0

Actually, turns out the first line is OK. It was the second line causing the crash. UITableView in Swift has the solution.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651