The map
method takes a closure as an argument, calls it for each item in the array, and returns a mapped value for the item. The returned mapped value can be of a different type from the item's type.
The following lines declare a new getUppercasedNames
method for our previously coded GameRepository
class that performs the simplest map operation. The code file for the sample is included in the swift_3_oop_chapter_07_21
folder:
open func getUppercasedNames() -> [String] { return getAll().map({ game in game.name.uppercased() }) }
The getUppercasedGames
parameterless method returns Array<String>
, specified with the [String]
shortcut. The code calls the getAll
method and calls the map
method for the result with a closure that returns the name
value for each game converted to uppercase. This way, the map
method transforms each Game
instance into String
with its name converted to uppercase. The result is an Array<String>
array generated by the call to the map
method.
The following line uses the GameRepository
instance called gameRepository
to call the previously added getUppercasedNames
method and then chains a call to forEach
to print all the game names converted to uppercase strings. The code file for the sample is included in the swift_3_oop_chapter_07_21
folder:
gameRepository.getUppercasedNames().forEach( { print($0) })
The following lines show the output generated by the preceding code:
INVADERS 2017 MINECRAFT MINECRAFT STORY MODE SOCCER WARRIORS THE WALKING DEAD STORIES ONCE UPON A TIME IN WONDERLAND CARS FOREVER JAKE & PETER PAN KONG STRIKES BACK MARIO KART 2017
The following code shows another version of the getUppercasedNames
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_22
folder:
open func getUppercasedNames() -> [String] {
return getAll().map({
(game: Game) -> String in
game.name.uppercased()
})
}
The following code shows another version of the getUppercasedNames
method, which is equivalent and produces the same results. The code file for the sample is included in the swift_3_oop_chapter_07_23
folder:
open func getUppercasedNames() -> [String] {
return getAll().map( { $0.name.uppercased() })
}
Swift supports tuples that group multiple values into a single compound value. The following lines declare a new getUppercasedAndLowercasedNames
method for our previously coded GameRepository
class, which performs a map
operation that returns a tuple, specifically, a tuple that groups two string values into a single compound value. The code file for the sample is included in the swift_3_oop_chapter_07_23
folder:
open func getUppercasedAndLowercasedNames() -> [(upper: String, lower: String)] { return getAll().map({ game -> (String, String) in (game.name.uppercased(), game.name.lowercased()) }) }
The getUppercasedAndLowercasedNames
parameterless method returns a tuple with two named String
values: [(upper: String, lower: String)
. The first string element in the tuple is named upper
, and the second one is named lower
. The code calls the getAll
and map
method for the result with a closure that returns a tuple with the first element equal to the name
value for each game converted to uppercase and the second element with the value converted to lower case. This way, the map
method transforms each Game
instance into a (String, String)
tuple with its name converted to uppercase and lowercase and stored in a compound value. The result is (String, String)
generated by the call to the map
method. The method declaration specifies names for each element in the returned tuple, so we will be able to access its members through these specified names.
The following line uses the GameRepository
instance called gameRepository
to call the previously added getUppercasedAndLowercasedNames
method and then chains a call to forEach
to print the upper
and lower
elements of the tuple separated by a hyphen. The code file for the sample is included in the swift_3_oop_chapter_07_23
folder:
gameRepository.getUppercasedAndLowercasedNames().forEach( { print($0.upper, " - ", $0.lower) })
The following lines show the output generated by the preceding code:
INVADERS 2017 - invaders 2017 MINECRAFT - minecraft MINECRAFT STORY MODE - minecraft story mode SOCCER WARRIORS - soccer warriors THE WALKING DEAD STORIES - the walking dead stories ONCE UPON A TIME IN WONDERLAND - once upon a time in wonderland CARS FOREVER - cars forever JAKE & PETER PAN - jake & peter pan KONG STRIKES BACK - kong strikes back MARIO KART 2017 - mario kart 2017
The following lines would produce the same results by accessing the tuple elements with .0
and .1
for the first and second elements instead of using the upper and lower names. The code file for the sample is included in the swift_3_oop_chapter_07_24
folder:
gameRepository.getUppercasedAndLowercasedNames().forEach( { print($0.0, " - ", $0.1) })
We can also easily iterate through the upper and lower pairs using a for
loop. The code file for the sample is included in the swift_3_oop_chapter_07_25
folder:
for (upper, lower) in gameRepository.getUppercasedAndLowercasedNames() { print("UPPER: (upper), lower: (lower)") }
The next lines show the results of executing the previous for
loop:
UPPER: INVADERS 2017, lower: invaders 2017 UPPER: MINECRAFT, lower: minecraft UPPER: MINECRAFT STORY MODE, lower: minecraft story mode UPPER: SOCCER WARRIORS, lower: soccer warriors UPPER: THE WALKING DEAD STORIES, lower: the walking dead stories UPPER: ONCE UPON A TIME IN WONDERLAND, lower: once upon a time in wonderland UPPER: CARS FOREVER, lower: cars forever UPPER: JAKE & PETER PAN, lower: jake & peter pan UPPER: KONG STRIKES BACK, lower: kong strikes back UPPER: MARIO KART 2017, lower: mario kart 2017
18.226.52.124