【发布时间】:2015-09-11 09:01:19
【问题描述】:
我正在尝试从我的数据源和以下代码行中删除一行:
if let tv = tableView {
导致以下错误:
条件绑定的初始化器必须是 Optional 类型,而不是 UITableView
这里是完整的代码:
// 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)
我应该如何更正以下内容?
if let tv = tableView {
【问题讨论】:
-
由于
tableView不是可选值,所以不需要检查它是否为nil。所以你可以直接使用它,我的意思是删除那个if let并在函数中使用tableView -
为了后代,在我解决了这个问题之后,我遇到了
variable with getter/setter cannot have an initial value,通过在初始化后简单地删除剩余的 { } 块来解决这个问题,这个答案:stackoverflow.com/a/36002958/4544328
标签: ios swift optional-binding