Here's a quick and easy use of filter()
var myArray = ["Explore","","#Swift","Algorithms"] var noEmptyStrings = myArray.filter({$0 != ""})
Filter can be called on an Array and simply requires that the Elements contained within the Array (in this case Strings) are tested in the closure and return a Bool value. Everything that returns true is then included in the filtered array, everything returning false is not.
See earlier posts on algorithms, generics and closures for further information and guidance.
Comments
Post a Comment