条件绑定:if let error – 条件绑定的初始化器必须有可选类型

Conditional Binding: if let error – Initializer for conditional binding must have Optional type

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

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 可选绑定仅在表达式右侧的结果为可选时有效。如果右侧的结果不是可选的,则不能使用此可选绑定。此可选绑定的要点是检查 nil 并且仅在变量不是 nil.

时才使用该变量

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

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.

对于我的具体问题,我不得不更换

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

let count = 1
if(count > 0)
    {
        // do something ...
    }

同样适用于 guard 语句。同样的错误消息将我引向这个 post 并回答(感谢@nhgrif)。

代码:仅当中间名少于四个字符时打印此人的姓氏。

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)!")
}

直到我将 last 声明为可选参数,我才看到同样的错误。

条件绑定必须有可选类型,这意味着您只能在 if let 语句中绑定可选值

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? {


        }
    }
}

这可以正常工作,但请确保当您使用 if let 时它必须具有可选类型“?”

如果您使用的是自定义单元格类型,例如 ArticleCell,您可能会收到一条错误消息:

    Initializer for conditional binding must have Optional type, not 'ArticleCell'

如果您的代码行如下所示,您将收到此错误:

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

您可以通过执行以下操作来修复此错误:

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

如果您检查上面的内容,您会发现后者正在对 ArticleCell 类型的单元格使用可选转换。

嗯,如果我们可以在 if 条件中声明通常的值,那么(在语法上)还是很方便的。所以,这里有一个技巧:你可以让编译器认为有一个 Optional.some(T) 赋值给这样的值:

    if let i = "abc".firstIndex(of: "a"),
        let i_int = .some(i.utf16Offset(in: "abc")),
        i_int < 1 {
        // Code
    }

它告诉您的是 - 2nd guard letif let check 没有发生在可选 Int 或可选字符串上。您已经有了 non-optional 值,因此不再需要保护或 if-letting

如果您检查文本字段,请确保可选文本?

  guard let msg = message.text?.trimmed,!messaged.isEmpty,messaged.count >= 14 else {
            MyVariables.status.image = UIImage(named: "wrong")
            MyVariables.status.message = "Enter Your Message".localized
            MyVariables.status.showInKeyWindow()
            return
        }