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 that we can use with the filter
method to retrieve only the games that match certain conditions. The following lines declare a new getGamesWithHighestScoreGreaterThan
method for our previously coded GameRepository
class:
public func getGamesWithHighestScoreGreaterThan(score: Int) -> [Game] { return getAll().filter({ (game) in game.highestScore > score }) }
The getGamesWithHighestScoreGreaterThan
method receives a score: Int
argument and returns Array<Game>
. The code calls the getAll
and filter
methods for the result with a closure that specifies the required condition for the games in the array to be returned in the new array. In this case, only the games whose highestScore
value is greater than the score
value received as an argument will appear in the resulting Array<Game>
...