Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何为if语句指定两个条件_Swift_If Statement - Fatal编程技术网

Swift 如何为if语句指定两个条件

Swift 如何为if语句指定两个条件,swift,if-statement,Swift,If Statement,我还是一个初学者,我正在尝试使用|作为“或”选项,但它不起作用 @IBAction func RunCode1(sender: AnyObject) { if TextField1.text == "true" || "false" { Outcome1.text = "false" CorrectText.hidden = false Reason.hidden = false C

我还是一个初学者,我正在尝试使用
|
作为“或”选项,但它不起作用

  @IBAction func RunCode1(sender: AnyObject) {
        if TextField1.text == "true" || "false" {
           Outcome1.text = "false"
            CorrectText.hidden = false
            Reason.hidden = false
            Continue1.hidden = false
    }    
当文本字段包含“真”或“假”时,我想做一些事情。

将其更改为:

if TextField1.text == "true" || TextField1.text == "false"
你的问题是:

 @IBAction func RunCode1(sender: AnyObject) {
        if TextField1.text == "true" || TextField1.text == "false" {
           Outcome1.text = "false"
            CorrectText.hidden = false
            Reason.hidden = false
            Continue1.hidden = false
    }   
另一种方式:):


请注意:属性、变量、方法名称应以小写字母开头。以大写字母开头仅适用于类/结构/协议等。请记住这一点。Thanks@Boudz78如果您必须比较两个以上的文本变量,您可以使用我的解决方案在我的情况下似乎很有帮助。。谢谢@khong291这是正确的!您必须记住,您不能说,例如,如果textField.text==“true”| |“false”。。。因为Xcode不知道看什么才能找到“false”
if let text = TextField1.text where ["true", "false"].contains(text) {
   //
}