EXC_BAD_INSTRUCTION breakpoint when starting app xCode - uitableview

I have a tableview in my app and when I start my app 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
}
It crashes on the line of var cell
It gives the following error:
I can't figure out what's 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 gives 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
}

This is happening because the as operator is defined to cast an object to a given type and crash if the conversion fails. In this case, the call to dequeue returns nil the first time you call it. You need to use the as? operator, which will attempt to cast the given object to a type, and return an optional that has a value only if the conversion succeeded:
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
}
...
Because cell is now an optional value, use cell! when you want to call methods on it to force-unwrap the UITableViewCell inside it.
Additionally, your code had a second problem: it never created a fresh cell. dequeue will return nil the first time it's called on your table view. You need to instantiate a new UITableViewCell as in my code sample and then return it from the cellFor... method. The table view will then save the cell and return it on future calls to dequeue.

First off, why are you doing an optional binding on line if let ip = indexPath? This argument is not optional and you don't need to do optional binding or unwrap it. But this shouldn't cause your code to crash.
Remove your let data line and assign literal strings to your cells and see if it still crashes.

May I suggest that you check to see if you set the tableview's delegates? I made that mistake once in the flurry of setting everything else up.

Perhaps it is too late but I like to share my experience. I had similar error as I copied the entire code from another project. So I think the variables and functions won't be recognised so I had to drag them (cntr+drag) then it is solved.
Sorry if I couldn't explain better. I am new this.

Related

What am i doing in the code below that is causing me to get errors?

I am getting following errors:
1) Non-optional expression of type 'UITableViewCell' used in a check for optionals
2) Value of type 'UITableViewCell' has no member 'congigureCell'
Please
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell // Error 1 happens here {
let text: String!
if inSearchMode {
text = filteredCountriesList[indexPath.row]
} else {
text = countriesList[indexPath.row]
}
cell.congigureCell(text: text) // Error 2 happens here
return cell
} else {
return UITableViewCell()
}
}
1) The ! mark at the end of
countryList.dequeueReusableCell(withIdentifier: "cell")!
uses force unwrap to make it non-optional, so you shouldn't check it inside if let, or even better way is to just remove ! mark
2) congigureCell probably the method of different class, not UITableViewCell. You should substitude UITableViewCell by this class to cast it
Make sure you have done following steps.
Add cell identifier in storyboard to your custom cell. i.e "cell"
Assign delegate and datasource of your YourTableview to YOURViewController.swift via storyboard or in code.
In YOURViewController.swift access cell using datasource of table
view as.
Add a custom class of sub class UITableViewCell and assign it to
tour cell in storyboard.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = countryList.dequeueReusableCell(withIdentifier: "cell") as! YOURTableViewCellClass {
let text: String!
if inSearchMode {
text = filteredCountriesList[indexPath.row]
} else {
text = countriesList[indexPath.row]
}
cell.congigureCell(text: text) // Error 2 happens here
return cell }
The ! mark is uses to force unwrap the optional value that can be nil. But "if let" and "guard let" has been check for optionals, so you don't need ! mark.
Just use
if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell
cell in this line is 'UITableViewCell', but congigureCell is not a member of UITableViewCell.
If you want to use your own cell(like MyCell), you should convert it to MyCell.
let myCell = cell as! MyCell
1 .Instead of dequeueReusableCellWithIdentifier: use dequeueReusableCellWithIdentifier:forIndexPath: the later one never provides a nil value so you dont need to worry about the 'nil error'.
2.UItableViewCell dont have configure cell or congigureCell as in your case instead you have to create a custom tableViewCell and add function as configureCell() and then in this line
if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
replace as UITableViewCell as as yourCustomTableViewCellClass

TableView Error Swift 2

I'm new to coding so I not sure how to fix this error:
private let cellReuseIdentifier = "ArticleViewCell"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as? UITableViewCell
if cell == nil {
cell = ArticleViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellReuseIdentifier)
}
I get this error:
Downcast from 'UITableViewCell?' to 'UITableView only warps optionals; did you mean to use "!"?
I tried changing the ? to ! put it get an error also.
Try
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! UITableViewCell
Also do not forget to return the cell.
return cell
You do not need to unwrap it at all:
var cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier)
Try:
let cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as! <ClassName>
Where "ClassName" is the name of the class for the article tableview cell.
after you create cell, when you use it you should add a "!" after the cell such as:
return cell!

Cannot convert from UITableViewCell to specified type

I am converting an app from objective-c to swift. I did not write the objective c code. One of the functions which code is as below...
func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier: String = "Cell"
var cell: ListCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)!
And theres more code in the function but i don't think it is relevant. When I run the app, I get an error: "nil found while unwrapping an optional" at the line where I create the variable cell. The error is occurring because I am trying to convert a UITableViewCell to ListCell type. And the conversion is occurring and returning nil. I need to have this line in the code because it is there in the objective-c version of the app.
Is there any way I can get around this and convert a UITableViewCell to ListCell without the conversion failing and returning nil?
Here is the declaration of the ListCell class...
#interface ListCell : UITableViewCell {
//code
}
I did not convert the ListCell class to swift, and instead use a bridging header. Also ListCell is a subclass of UITableViewCell, so why is the conversion not working.
There are two ways you can use dequeueReusableCellWithIdentifier - The most common "modern" way is to have registered the reuse identifier and specified a custom class for your cell in your storyboard. You can also use the UITableView method registerClass:forCellReuseIdentifier to do the same thing programatically.
tableview.registerClass(ListCell.self, forCellReuseIdentifier:"Cell")
In this case you would say
var cell=tableview.dequeueReusableCellWithIdentifier(cellIdentifier) as! ListCell
You can use a forced downcast because you know that the dequeue operation will return a cell of the appropriate type (and if not then you have a problem and throwing an exception is good so you can debug it).
The "old" way was not to register a class and deal with the fact that dequeueReusableCellWithIdentifier may return nil - In this case you need to allocate a new cell yourself;
var cell:Listcell
if let reuseCell = tableview.dequeueReusableCellWithIdentifier(cellIdentifier) as? ListCell {
cell=reuseCell
} else {
cell=ListCell(style: .Default, reuseIdentifier: cellIdentifier)
}
// Now you can configure cell...
Try this, it should work fine.
func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellIdentifier: String = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! ListCell
You should use this version:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",
forIndexPath: indexPath) as! ListCell
The dequeue method version with index path never returns nil (creates new cells automatically, when needed).
The force cast as! either succeeds with a non-optional value, or crashes the app (in this case if the cell was not actually ListCell).
it should be
var cell: ListCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! ListCell

(Swift) tableView application iOS fails because of "Bound value in a conditional binding must be of Optional type"

I am trying to create a database-based application for iOS and I am, more or less, forced to use Xcode 6.2 Beta 3, because I started the project with 6.2 and run iOS 8.2 on my testdevice. I have by the way figured out that the beta forces me to use the standard keypad, instead of decimal pad, otherwise the app stops working.
I canĀ“t figure out what is wrong. And when I try to comment the function out something is still wrong in this file. Are there any missing connections between the VC-swift-file, tableVC-swift-file and datamodel-file?
I changed the name of the entity list once, and then changed back, because of another error I had before. But after I did it seems like the debug area doesnt catch the data I try to save to the database either...
The error says "Bound value in a conditional binding must be of Optional type"
Here is the code that is marked red on the initial line:
if let ip = indexPath {
var data: NSManagedObject = tankningslista[ip.row] as NSManagedObject
cell.textLabel?.text = data.valueForKey("datum") as String
}
The whole function looks like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
let CellID: NSString = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell
if let ip = indexPath {
var data: NSManagedObject = tankningslista[ip.row] as NSManagedObject
cell.textLabel?.text = data.valueForKey("datum") as String
}
return cell
}
I have followed this tutorial: https://www.youtube.com/watch?v=4ymz6i07DRM
try this way without using condition:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
let CellID: NSString = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell
var data: NSManagedObject = tankningslista[indexPath.row] as NSManagedObject
cell.textLabel.text = data.valueForKey("datum") as? String
return cell
}

Overriding UITableView in Parse with Swift

All,
In swift while using Parse as my backend, I have created a class which inherits from PFQueryTableViewController. I see my data going into a tableview - thats fine.
I am trying to customise my cells a bit, and I overriding CellForRowAtIndexPath - like below :
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
{
var cellIdentifier = "eventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? PFTableViewCell
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
}
// configure cell
var CellTitle = object.objectForKey("Title") as String
cell?.textLabel = CellTitle
}
}
As the object using comes back as [AnyObject] in swift, I have created a variable and I have casted it to a string. And then I am trying to show that string in Cell.textlabel.
I am getting the error : Cannot assign to the result of this expression.
Can anyone please show me the right direction on this.
I think the problem is that you're attempting to assign a String directly to cell?.textLabel UILabel. Instead try changing this line:
cell?.textLabel = CellTitle
to this
cell.textLabel?.text = CellTitle
so you're setting the text property of the UILabel instead.

Resources