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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.