nyitguy nyitguy - 3 months ago 15
Swift Question

Swift arrays of strings

I have an array of strings.

var animals = ["cats 99", "dogs 42", "chimps 45", "moose 98"]
var excludeAnimalArray = ["chimps", "dogs"]


Output should be "cats 99", "moose 98". Please keep in mind there is no way to tell what the number next to the animal will be.

Answer

A shorter filter:

let result = animals.filter { animal in
    !excludeAnimalArray.contains { animal.hasPrefix($0) }
}
Comments