SimonTheEngineer SimonTheEngineer - 1 year ago 124
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.

Answer Source

You can use the contains() method:

var strings = ["Test", "hi"]
if contains(strings , "hi") {
    println("Array contains string")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download