Search code examples
arraysswiftremove-if

(Swift) What is the easiest way to delete all elements in array when condition is true


setsOfCardsByLevel is an array of cards.
Every card has a level.
I want to remove all cards from array if their level is equal to level from the parameter. Here is my function:

 func removeCardsByLevel(_ level: Int) {
            for card in setsOfCards {
                if card.level == level {
                    setsOfCards.remove // HOW?
                }
              }
          }

Advices? Ideas?


Solution

  • If I understand you correctly, you wish to remove the cards that matches a certain level?

    setsOfCards.removeAll { $0.level == level }