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 games whose highestScore
value is greater than the score value received as an argument will appear in the resulting Array<Game>
.
The following lines use the GameRepository
instance called gameRepository
to call the previously added method and then chain a call to forEach
to print all the games whose highestScore
value is greater than 5,000,000. The code file for the sample is included in the swift_3_oop_chapter_07_13
folder:
gameRepository.getWithHighestScoreGreaterThan (score: 5_000_000).forEach( { print($0) })
The following lines show the output generated using the preceding code:
id: 3, name: "Minecraft Story Mode", highestScore: 67881050, playedCount: 304506506 id: 7, name: "Cars Forever", highestScore: 6705203, playedCount: 850021 id: 10, name: "Mario Kart 2017", highestScore: 10572340, playedCount: 3760879
The following code shows another version of the getWithHighestScoreGreaterThan
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_14
folder:
open func getWithHighestScoreGreaterThan(score: Int) -> [Game] { return getAll().filter({ (game: Game) -> Bool in game.highestScore > score }) }
The following code shows another version of the getWithHighestScoreGreaterThan
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_15
folder:
open func getWithHighestScoreGreaterThan(score: Int) -> [Game] {
return getAll().filter({ $0.highestScore > score })
}
The following lines declare a new getWith
method for our previously coded GameRepository
class. The code file for the sample is included in the swift_3_oop_chapter_07_16
folder:
open func getWith(prefix: String) -> [Game] { return getAll().filter({ game in game.name.hasPrefix(prefix) }) }
The getWith
method receives a prefix
, String
argument and returns an Array<Game>
, specified with the [Game]
shortcut. The code calls the getAll
method and calls the filter
method 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 name includes the string specified in the prefix
value and is received as an argument or prefix will appear in the resulting Array<Game>
.
The following line uses the GameRepository
instance called gameRepository
to call the previously added method and then chains a call to forEach
to print all the games whose name starts with "Mi"
. The code file for the sample is included in the swift_3_oop_chapter_07_16
folder:
gameRepository.getWith(prefix: "Mi").forEach( { print($0) })
The following lines show the output generated by the preceding code:
id: 2, name: "Minecraft", highestScore: 3741050, playedCount: 780009992 id: 3, name: "Minecraft Story Mode", highestScore: 67881050, playedCount: 304506506
The following code shows another version of the getWith
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_17
folder:
open func getWith(prefix: String) -> [Game] { return getAll().filter({ (game: Game) -> Bool in game.name.hasPrefix(prefix) }) }
The following code shows another version of the getWith
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_18
folder:
open func getWith(prefix: String) -> [Game] {
return getAll().filter({ $0.name.hasPrefix(prefix) })
}
So far, we have used the filter
method to generate a new Array<Game>
. Sometimes, we just want to retrieve a single element from an Array
or a similar collection, and we also want to specify a more complex condition. The following lines declare a new getBy
method for our previously coded GameRepository
class. The code file for the sample is included in the swift_3_oop_chapter_07_18
folder:
open func getBy(highestScore: Int, playedCount: Int) -> Game? { return getAll().filter({ game in game.highestScore == highestScore && game.playedCount == playedCount }).first }
The getBy
method receives two Int
arguments: highestScore
and playedCount
. The method returns an optional Game
, that is, 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
and playedCount
values are equal to the values received as arguments with the same names will appear in the Array<Game>
generated by the call to the filter
method. Then, the call to the first method returns the first
element in the generated array or nil
if no elements are found.
The following lines use the GameRepository
instance called gameRepository
to call the previously added method to retrieve two games that match the specified highestScore
and playedCount
values. The method returns a Game?
; therefore, the code checks whether the result is a Game
instance or not in each call using if
statements. The code file for the sample is included in the swift_3_oop_chapter_07_18
folder:
if let game0 = gameRepository.getBy(highestScore: 4023134, playedCount: 350230) { print(game0) } else { print("No game found with the specified criteria") } if let game1 = gameRepository.getBy(highestScore: 30, playedCount: 40) { print(game1) } else { print("No game found with the specified criteria") }
The following lines show the output generated with the preceding code. In the first call, there was a game
that matched the search criteria. In the second call, there is no Game
instance included in the array that matches the search criteria:
id: 8, name: "Jake & Peter Pan", highestScore: 4023134,
playedCount: 350230
No game found with the specified criteria
The following code shows another version of the getBy
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_19
folder:
open func getBy(highestScore: Int, playedCount: Int) -> Game? { return getAll().filter({ (game: Game) -> Bool in game.highestScore == highestScore && game.playedCount == playedCount }).first }
The following code shows another version of the getBy
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_20
folder:
open func getBy(highestScore: Int, playedCount: Int) -> Game? { return getAll().filter({ $0.highestScore == highestScore && $0.playedCount == playedCount }).first }
3.16.76.227