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. 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...