Filtering arrays with complex conditions

We can use our new repository to restrict the results retrieved from more complex data. In this case, the getAll method returns an array of Game instances, which we can use with the filter method to retrieve only the games that match certain conditions. The following lines declare a new getWithHighestScoreGreaterThan method for our previously coded GameRepository class. The code file for the sample is included in the swift_3_oop_chapter_07_13 folder:

    open func getWithHighestScoreGreaterThan(score: Int) -> [Game] { 
        return getAll().filter({ (game) in game.highestScore > score }) 
    } 

The getWithHighestScoreGreaterThan method receives a score: Int argument and returns Array<Game>, specified with the [Game] shortcut. ...

Get Swift 3 ObjectOriented Programming - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.