Conditional binding: if an error - the initializer for the conditional binding must be of an optional type

I am trying to remove a line from my data source and the following line of code:

if let tv = tableView { 

causes the following error:

The conditional binding initializer must be an optional type, not a UITableView

Here is the complete code:

 // Override to support editing the table view. func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source if let tv = tableView { myData.removeAtIndex(indexPath.row) tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

How do I fix the following?

  if let tv = tableView { 
+65
ios swift optional-binding
Jun 24 '15 at 23:21
source share
5 answers

if let / if var optional binding only works when the result of the right side of the expression is optional. If the result of the right side is not optional, you cannot use this optional binding. The point of this optional binding is to check for nil and use this variable only if it is not nil .

In your case, the tableView parameter tableView declared as an optional type of UITableView . He will never be nil . Therefore, optional binding is not required here.

 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 need to do is get rid of if let and change any occurrences of tv inside it only to tableView .

+136
Jun 25 '15 at 0:43
source share

for my particular problem i had to replace

 if let count = 1 { // do something ... } 

FROM

 let count = 1 if(count > 0) { // do something ... } 
+11
Nov 08 '15 at 22:07
source share

The same applies to guard statements. The same error message led me to this post and answer (thanks @nhgrif).

Code: print the personโ€™s last name only if the middle name is less than four characters.

 func greetByMiddleName(name: (first: String, middle: String?, last: String?)) { guard let Name = name.last where name.middle?.characters.count < 4 else { print("Hi there)") return } print("Hey \(Name)!") 

}

Until I declared last as an optional parameter, I saw the same error.

+7
Sep 11 '16 at 15:44
source share

binding must be of type optinal, which means you can only bind optional values โ€‹โ€‹if the let statement

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // Delete the row from the data source if let tv = tableView as UITableView? { } } } 

This will work fine, but make sure that when you use it, if it should be of the optical type โ€œ?โ€

+1
Mar 20 '17 at 8:01
source share

In the case when you use a custom cell type, say ArticleCell, you may receive an error message:

  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 following these steps:

  if let cell = tableView.dequeReusableCell(withIdentifier: "ArticleCell",for indexPath: indexPath) as ArticleCell? 

If you check the above, you will see that the latter uses optional casting for a cell of type ArticleCell.

0
Nov 01 '17 at 9:08 on
source share



All Articles