SimonTheEngineer SimonTheEngineer - 1 month ago 4x
Swift Question

Compare String against String Array

I'm currently trying to determine the best way to compare a string value to an array of Strings. Here's the problem...

I'm building a converter between binary, decimal and hex values that share the same keypad. I want to check the input and depending on the mode, let it through or not (e.g. binary more only allows 1 and 0, decimal 0-9 and hex 0-F).

I can do this by saying:

if (digit == binaryDigits[0]) || (digit == binaryDigits[1]) {
// do something

but that's not very practical when it comes to the decimal and hex values.

Basically I would like a way to check a given String against all values within a String Array.


You can use the contains() method:

var strings = ["Test", "hi"]
if contains(strings , "hi") {
    println("Array contains string")