gpt4 book ai didi

ios - 条件绑定(bind) : if let error – Initializer for conditional binding must have Optional type

转载 作者:行者123 更新时间:2023-11-28 08:26:33 24 4
gpt4 key购买 nike

我正在尝试从我的数据源中删除一行和以下代码行:

if let tv = tableView {

导致以下错误:

Initializer for conditional binding must have Optional type, not 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 {

最佳答案

if let/if var可选绑定(bind)仅在表达式右侧的结果为可选时才有效。如果右侧的结果不是可选的,则不能使用此可选绑定(bind)。这个可选绑定(bind)的要点是检查 nil并且只使用非 nil 的变量.

在你的例子中,tableView参数声明为非可选类型 UITableView .保证永远不会是nil .所以这里的可选绑定(bind)是不必要的。

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)

我们所要做的就是摆脱 if let并更改任何出现的 tv在里面只有tableView .

关于ios - 条件绑定(bind) : if let error – Initializer for conditional binding must have Optional type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com