I am trying to delete a row from my Data Source and the following line of code:if let tv = tableView {causes the following error:Here is the full 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 should I correct the following? if let tv = tableView { 解决方案 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. 这篇关于条件绑定:如果让错误 - 条件绑定的初始值设定项必须具有 Optional 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 21:47