Swift-如果文本与数组中的任何字符串完全匹配

时间:2018-08-29 15:04:37

标签: ios arrays swift if-statement uitextfield

我正在尝试查看UITextField.text是否与数组中的任何字符串完全匹配。当我使用字母时,我需要知道文本是否完全匹配,而不仅仅是包含数组中的元素。

在if语句中,我不确定要在“ cameraBodies”之后搜索整个数组吗?

谢谢您的帮助

let cameraBodies = ["A","B","C","D","E","F","G","H","I","J","K",
                    "L","M","N","O","P","Q","R","S","T","U","V",
                    "W","X","Y","Z","Other"]

if cameraBodyTextField.text != cameraBodies (Here is the problem) {     
            print("Does not match")
        } else {    
            print("Does match")
        }
}

1 个答案:

答案 0 :(得分:1)

使用if cameraBodies.contains(cameraBodyTextField.text!) { ... }
如需更多帮助,请查看此array contains.