1

I have a viewController that has a variable and it's getting the value of this variable from another view controller so the variable is like this for example..

var name = "Step-Up to Emergency Medicine"

and there is a button when clicked it's append this string to an arraylist

@IBAction func add(_ sender: Any) {
        myArray.append(name)
}

my question is how to prevent the arraylist from adding the same string value twice?

0

1 Answer 1

1

Make sure the string isn't already in the array before appending, like this:

@IBAction func add(_ sender: Any) {
   if !myArray.contains(name) {
       myArray.append(name)
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Better to use a set but if your list contains just a few elements you won't notice any difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.