O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required