adams.s adams.s - 4 months ago 5x
Swift Question

Possibilities Array's (swift)

How can i use more values of an array in a swift statement?


var array = ["Beer", "Water", "Cola", "Sprite", "Wodka"]

var drinks = "" // The variable drinks updates with a pickerView of the above array

func doSomethingWithArray() {

if drinks == array[1] {

print("this is water, so no sugar in it")

} else if drinks == array[2,3] {

print("This drink contains sugar")

} else {
print("This drink is alcoholic")

Is there an easy way to use multiple array values like in the else if statement? but a way that works then. :)


After following the conversation in the comments, this code should work

struct Drink {
    var name: String
    var haveSugar: Bool
    var haveAlcohol: Bool

    init (name: String, haveSugar: Bool, haveAlcohol: Bool) { = name
        self.haveSugar = haveSugar
        self.haveAlcohol = haveAlcohol

var drinks = [Drink(name: "Beer", haveSugar: true, haveAlcohol: true), Drink(name: "Water", haveSugar: false, haveAlcohol: false), Drink(name: "Sprite", haveSugar: true, haveAlcohol: false), Drink(name: "Wodka", haveSugar: true, haveAlcohol: true)]

func checkDrink(drink: Drink) {
    if drink.haveSugar == true {
        print("\( have sugar!")
    else {
        print("\( doesn't have sugar")