Conditional Binding: if let error – Initializer for conditional binding must have Optional type
if let
/if var
optional binding only works when the result of the right side of the expression is an optional. If the result of the right side is not an optional, you can not use this optional binding. The point of this optional binding is to check for nil
and only use the variable if it's non-nil
.
In your case, the tableView
parameter is declared as the non-optional type UITableView
. It is guaranteed to never be nil
. So optional binding here is unnecessary.
func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
myData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
All we have to do is get rid of the if let
and change any occurrences of tv
within it to just tableView
.
for my specific problem I had to replace
if let count = 1
{
// do something ...
}
With
let count = 1
if(count > 0)
{
// do something ...
}
In a case where you are using a custom cell type, say ArticleCell, you might get an error that says :
Initializer for conditional binding must have Optional type, not 'ArticleCell'
You will get this error if your line of code looks something like this:
if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as! ArticleCell
You can fix this error by doing the following :
if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as ArticleCell?
If you check the above, you will see that the latter is using optional casting for a cell of type ArticleCell.