John Ramos John Ramos - 4 months ago 17
Swift Question

Filter an array by NOT containing string (swift 2)

I am relatively new to swift; I am working on filtering arrays.
I know how to filter out elements of an array that contain a letter (like so:

let filteredList = wordlist.filter { !$0.characters.contains(letter) }
), but how do I filter out elements that do not have a letter?
Here's what I want to accomplish:
I have a word list in string-array format, i.e. ["thing", "other thing"] (but much longer), and I want to return every element that has a certain letter, filtering out the ones that do not have a certain letter.

Thanks in advance.

Answer

This was a silly question, I am sorry. Anyway, I just needed to remove the exclamation mark. So...

 let filteredList = wordlist.filter { !$0.characters.contains(letter) } 
 // returns elements in the array WITHOUT "letter".
 let filteredList = wordlist.filter { $0.characters.contains(letter) }  
 // returns elements in the array WITH "letter".

Thanks Eendje.

Comments