O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition 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, 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. ...

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