ma11hew28
ma11hew28

Reputation: 126507

EXC_BAD_INSTRUCTION runtime error

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

Upvotes: 0

Views: 604

Answers (2)

ma11hew28
ma11hew28

Reputation: 126507

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

Upvotes: 0

artey
artey

Reputation: 1243

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)

Upvotes: 1

Related Questions