简体   繁体   English

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

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

I am doing an UIAlertController and I got this message: "Non-optional expression of type uitextfield used in a check for optionals". 我正在做一个UIAlertController,并收到以下消息:“用于检查可选项的uitextfield类型的非可选表达式”。 This is in the line where I have the "if let my field". 这是我拥有“如果让我的领域”的行。

I got this code: 我得到以下代码:

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

Can anyone tell me how to remove this warning please? 谁能告诉我如何删除此警告? I tried to remove the "!" 我试图删除“!” with no success. 没有成功。

You are forced unwrapping the value so it's a non-optional. 您必须解开该值,因此它是非可选的。 That's what the message tells you. 这就是该消息告诉您的内容。

Since you have definitely added the text field to the alert controller you don't need any check and the code can be shortened: 由于您确实已将文本字段添加到了警报控制器,因此您无需进行任何检查,并且可以缩短代码:

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

No type annotation, no type casting, the compiler can infer the type. 没有类型注释,没有类型转换,编译器可以推断类型。

To check for empty string there is also a more convenient syntax: 要检查空字符串,还有一种更方便的语法:

if !field.text.isEmpty { ...

You don't need to do the cast, the type of alertController.textFields is already [UITextField] . 您无需进行alertController.textFieldsalertController.textFields的类型已经是[UITextField] Adding the cast is what's really causing you the problem. 真正导致问题的原因是添加演员表。

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
        }
    }
}

I would like to show you something more. 我想向您展示更多。 You don't need to set the type and I try to make my conditionals as specific as possible. 您不需要设置类型,我会尝试使条件尽可能具体。

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
    }
}

In addition, I think I would use a guard here. 另外,我想我会在这里使用警卫。

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
}

Doing (alertController.textFields? [0])! as UITextField 在做(alertController.textFields? [0])! as UITextField (alertController.textFields? [0])! as UITextField , don't really make sense, because it means that you are trying to cast alertController.textFields[0] to a UITextField , when it is already a type of UITextField . (alertController.textFields? [0])! as UITextField ,实际上没有任何意义,因为这意味着您正在尝试将alertController.textFields[0]转换为UITextField时,它已经是一种UITextField

What you should do instead is to check if the first element of alertController.textFields exist, which you can do so with the code below. 相反,您应该做的是检查alertController.textFields的第一个元素alertController.textFields存在,您可以使用下面的代码来做到这一点。

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

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

相关问题 用于检查选项的“字符串”类型的非可选表达式 - Non-optional expression of type 'String' used in a check for optionals 检查可选内容时使用的'AnyObject'类型的非可选表达式 - Non-optional expression of type 'AnyObject' used in a check for optionals Swift 3警告:检查选项时使用的“String”类型的非可选表达式 - Swift 3 warning: Non-optional expression of type 'String' used in a check for optionals NSDictionary类型的非可选表达式,用于检查可选项。 错误 - Non optional expression of type NSDictionary used in a check for optionals. Error 检查 NSSize 是否为零 - 比较“NSSize”类型的非可选值? - Check if NSSize is nil - Comparing non-optional value of type 'NSSize'? Swift比较Strings选项与非选项 - Swift comparing Strings optionals vs non-optional Xcode NSManagedObject 子类在标记为非可选时包含可选 - Xcode NSManagedObject subclass contains optionals when they are marked as non-optional 将可选类型转换为非可选类型 - Convert an optional Type to non-optional Type 扩展可选和非可选类型 - Extend Optional and Non-Optional Type nil 合并运算符 '??' 的左侧具有非可选类型“字符串”,因此从不使用右侧 - Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM