繁体   English   中英

uitextfield类型的非可选表达式,用于检查可选项

[英]Non-optional expression of type uitextfield used in a check for optionals

我正在做一个UIAlertController,并收到以下消息:“用于检查可选项的uitextfield类型的非可选表达式”。 这是我拥有“如果让我的领域”的行。

我得到以下代码:

let alertController = UIAlertController(title: "Adding", message: "Type something", preferredStyle: UIAlertControllerStyle.alert)
let DestructiveAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in

if let field : UITextField = (alertController.textFields? [0])! as UITextField  {

 if field.text != "" {
//my actions                
        }

    }
    let okAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
    }
    alertController.addTextField { (textField) in
        textField.placeholder = "Type designation here"

    }
    alertController.addAction(okAction)
    alertController.addAction(DestructiveAction)
    self.present(alertController, animated: true, completion: nil)
}

谁能告诉我如何删除此警告? 我试图删除“!” 没有成功。

您必须解开该值,因此它是非可选的。 这就是该消息告诉您的内容。

由于您确实已将文本字段添加到了警报控制器,因此您无需进行任何检查,并且可以缩短代码:

let field  = alertController.textFields![0] // or textFields.first!

没有类型注释,没有类型转换,编译器可以推断类型。

要检查空字符串,还有一种更方便的语法:

if !field.text.isEmpty { ...

您无需进行alertController.textFieldsalertController.textFields的类型已经是[UITextField] 真正导致问题的原因是添加演员表。

let alertController = UIAlertController(title: "Adding", message: "Type something", preferredStyle: UIAlertControllerStyle.alert)
let DestructiveAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
    if let field : UITextField = alertController.textFields?[0]  {
        if field.text != "" {
            //my actions
        }
    }
}

我想向您展示更多。 您不需要设置类型,我会尝试使条件尽可能具体。

let alertController = UIAlertController(title: "Adding", message: "Type something", preferredStyle: UIAlertControllerStyle.alert)
let DestructiveAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
    if let text = alertController.textFields?[0].text, !text.isEmpty {
        //my actions
    }
}

另外,我想我会在这里使用警卫。

let alertController = UIAlertController(title: "Adding", message: "Type something", preferredStyle: UIAlertControllerStyle.alert)
let DestructiveAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
    guard let text = alertController.textFields?[0].text, !text.isEmpty else {
        return
    }

    //my actions
}

在做(alertController.textFields? [0])! as UITextField (alertController.textFields? [0])! as UITextField ,实际上没有任何意义,因为这意味着您正在尝试将alertController.textFields[0]转换为UITextField时,它已经是一种UITextField

相反,您应该做的是检查alertController.textFields的第一个元素alertController.textFields存在,您可以使用下面的代码来做到这一点。

if let field : UITextField = alertController.textFields.first  {
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM