🌐
Apple Developer
developer.apple.com › documentation › swift › sequence › map(_:)
map(_:) | Apple Developer Documentation
Returns an array containing the results of mapping the given closure over the sequence’s elements.
🌐
Hacking with Swift
hackingwithswift.com › example-code › language › how-to-use-map-to-transform-an-array
How to use map() to transform an array - free Swift example code and tips
May 28, 2019 - Swift version: 5.10 · Paul Hudson @twostraws May 28th 2019 · The map() method allows us to transform arrays (and indeed any kind of collection) using a transformation closure we specify.
Discussions

Question re .map and closures - Using Swift - Swift Forums
I'm reading a book with tutorials in Swift and in it, they list the following code: class Person { var firstName: String var lastName: String } Based on that class definition, it then lists the following code which I do not understand: var imposters = (0...100).map { _ in Person(firstName: ... More on forums.swift.org
🌐 forums.swift.org
0
March 24, 2020
When to use map vs. for loop?
Use map to transform collections of things. Use for loops to "do something" for each thing in a collection. More on reddit.com
🌐 r/swift
23
9
December 31, 2014
Iterating array of array in swift using map - Stack Overflow
I am trying to udnerstand the concept of map function in swift. I want to iterate an array of array in such a way that I can print each element in every array along with it's index. Below is my cod... More on stackoverflow.com
🌐 stackoverflow.com
How to center map on user location when app is opened?
Without having worked with MapKit before, I would say you need to first get the user’s CLLocation to identify where to place the center of the map. There should be a function or property on the MapKit protocol that you can use to center the map on the location. More on reddit.com
🌐 r/swift
5
13
March 3, 2019
🌐
Cocoa Casts
cocoacasts.com › swift-essentials-1-how-to-use-swift-map-to-transforms-arrays-sets-and-dictionaries
How to Use Swift Map to Transform Arrays, Sets, and ...
import Foundation let strings = Set([ "one", "two", "three" ]) let ints = strings.map { $0.count } print(ints) Run the contents of the playground to see the result. You can verify that the order of the elements is undefined if you run the contents of the playground several times. It should be no surprise that Swift's map(_:) method can also be used to transform the keys and values of a dictionary.
🌐
Medium
medium.com › @danielbanales › using-the-map-function-in-swift-to-transform-collections-69f34b0fc943
Using the Map Function in Swift to Transform Collections | by Daniel BR | Medium
January 10, 2023 - You probably don’t like the fact are optional and prefer to ignore the nil values; Swift already provides us with a standard way to achieve this, using the compactMap method. This method allows you to create an array, that discards nil values; thus, the type of the elements would never be optional. You can also chain methods to make more complex operations. In the above example, first, we convert the Strings to Integers, using compactMap to discard any possible nil values, and then we use map to multiply each element by 2.
🌐
Esri Developer
developers.arcgis.com › swift
ArcGIS Maps SDK for Swift | Esri Developer
January 1, 2026 - This guide describes how to use ArcGIS Maps SDK for Swift to build mobile apps that incorporate capabilities such as mapping, geocoding, routing, and spatial analysis.
🌐
Programiz
programiz.com › swift-programming › library › array › map
Swift Array map() (With Examples)
The map() method transforms the array by applying the same operation to each element in the array. The map() method transforms the array by applying the same operation to each element in the array. Example var numbers = [1, 2, 3, 4] // add 2 to each element var result = numbers.map({ $0 + 2}) ...
🌐
Codecademy
codecademy.com › docs › swift › arrays › .map()
Swift | Arrays | .map() | Codecademy
November 22, 2022 - The .map() method returns a new array containing the transformed values from calling the passed-in closure on each element of the given collection. It can be used on types that follow Swift’s Sequence and Collection protocols such as arrays, ...
Find elsewhere
🌐
Google
developers.google.com › google maps platform › ios › maps sdk for ios › add a map to your ios app (swift)
Add a map to your iOS app (Swift) | Maps SDK for iOS | Google for Developers
November 11, 2024 - The most commonly used part of the Maps SDK for iOS is the GMSMapView class, which provides many of the methods that allow you to create and manipulate map instances. Here's how that's done: Open ViewController.swift.
🌐
Southwest Florida Water Management District
swfwmd.state.fl.us
Welcome to the Southwest Florida Water Management District Home Page
Data & Maps · Show Me Around · Projects & Programs · Learn More · Meetings & Events Calendar · View What's Coming Up · District Newsroom · Get the Latest News · Recreation · Get Outside! Prescribed Burns · Learn More · February 27, 2026 ·
🌐
Medium
mehrdad-ahmadian.medium.com › understanding-map-flatmap-and-compactmap-in-swift-eacafc38fb61
Understanding map, flatMap, and compactMap in Swift | by Mehrdad Ahmadian | Medium
December 24, 2023 - Understanding when and how to use map, flatMap, and compactMap can significantly enhance your Swift code's readability and efficiency. map is your go-to for simple transformations, flatMap for dealing with nested collections or optionals, and ...
🌐
Metro
metro.co.uk › home › news
Taylor Swift puts Croydon shopping centre on the map in Opalite video | News UK | Metro News
3 weeks ago - Some locals praised Swift for putting Croydon ‘on the map’, though others fumed at reading articles describing the shopping centre as ‘abandoned’.
🌐
Hacking with Swift
hackingwithswift.com › plus › functional-programming › transforming-data-with-map
Transforming data with map() – Hacking with Swift+
In this article we’re going to look at the map() function, which transforms one thing into another thing. Along the way we’ll also be exploring some core concepts of functional programming, so if you read no other articles in this course at least read this one! This is a free sample from Hacking with Swift+, a subscription that delivers high-quality tutorials as both articles and videos.
🌐
DhiWise
dhiwise.com › post › exploring-the-intricacies-of-swift-map-dictionary
The Ultimate Swift Map Dictionary Tutorial for Beginners
July 10, 2024 - In Swift, ‘map’ is a powerful function used extensively in array manipulation. The map function applies a given transformation to each element of an array, creating a new array with the transformed elements.
🌐
DhiWise
dhiwise.com › post › swift-map-array-simplifying-transformations-in-swift
Enhance Your Development Workflow with Swift Map Array
June 18, 2024 - The map function returns a new array, which consists of these transformed elements. ... Swift array map is a highly valuable method due to its broad applicability and efficiency in sorting through large arrays.
🌐
Swift Forums
forums.swift.org › using swift
Question re .map and closures - Using Swift - Swift Forums
March 24, 2020 - I'm reading a book with tutorials in Swift and in it, they list the following code: class Person { var firstName: String var lastName: String } Based on that class definition, it then lists the following code which I do not understand: var imposters = (0...100).map { _ in Person(firstName: "John", lastName: "Appleseed") } As per the last block of code, is it creating a numeric range of 0 to 100, and within that numeric range, creating an object with the properties firstName and lastName, thu...
🌐
Mokacoding
mokacoding.com › blog › when-to-use-map-flatmap-for
When to use map, flatMap, or for loops in Swift | mokacoding
October 5, 2015 - Swift allows us to natively iterate over arrays using map. Map could be used to replace every for loop in your code, but that's not a great idea. Map and for have different purposes and should be used appropriately
🌐
Reddit
reddit.com › r/swift › when to use map vs. for loop?
r/swift on Reddit: When to use map vs. for loop?
December 31, 2014 -

Been playing around with map, flatMap, filter, reduce, forEach, etc. and love the syntax of them all. The first thought I had after discovering map was "Why would I ever need a for loop / forEach if I can just use map?"

Did some google searches and found a few discussions but they were either lacking in info or too low level for me to understand. I'm basically interested in performance and following best practices.

If anyone here knows the answer and could ELI5 I would really appreciate it. I want to start making use of all of these new functions but don't want to go overboard either.

Thanks and I appreciate the help.

Top answer
1 of 2
1

Let's start with a single array, like:

Copylet raceResult = ["one", "two", "four"]

If we want to combine each element with an offset counting from 0, we can use Array.enumerated(), along with map.

Copylet numberedRaceResult = raceResult
    .enumerated()
    .map { offset, element in "\(offset). \(element)" }

for numberedResult in numberedRaceResult {
    print(numberedResult)
}

// Prints:
// 0. one
// 1. two
// 2. four

You can see that I didn't call print inside the closure passed to map. You can do this, but it kind of defeats the purpose of map (which is to create an equal-sized output array from the transformed elements of the input array), because the result would be unused. In that case, it makes more sense to just use a for loop or a call to forEach, like @Sh_Khan showed.

To handle a nested array, it's much the same. We can use the same logic as for one array, but apply it to each sub-array.

Copylet raceResults = [
    ["one", "two", "four"],
    ["two", "one", "five", "six"],
    ["two", "one", "four", "ten"],
    ["one", "two", "four"],
]

let numberedRaceResults = raceResults
    .enumerated()
    .flatMap { outterOffset, raceResult in
        raceResult
            .enumerated()
            .map { innerOffset, element in "\(outterOffset).\(innerOffset). \(element)" }
    }

for numberedResult in numberedRaceResults {
    print(numberedResult)
}

// Prints:
// 0.0. one
// 0.1. two
// 0.2. four
// 1.0. two
// 1.1. one
// 1.2. five
// 1.3. six
// 2.0. two
// 2.1. one
// 2.2. four
// 2.3. ten
// 3.0. one
// 3.1. two
// 3.2. four

You'll notice that I used flatMap on the outter array, instead of a simple map. You can change it back and forth and compare the result. In short, flatMap gives you a single flat array of string results, rather than an array of sub-arrays of strings.

2 of 2
1

Map is used to convert one bunch of type T into things of some other type, X. Like map these Ints to String?s. You should not use map for side-effects, like printing the values, or updating a database etc. It should be a pure function that takes an input and returns an output. "Map these A's into B's". Pure meaning the value of the function only depends on the input, nothing else like the current state of the world, and doesn't change the world either (like printing to the console), so for example, map these int's by the function that adds 2 to them.

In your example:

Copyvar raceResults = [["one","two","four"],["two","one","five","six"],["two","one","four","ten"],["one","two","four"]]

You have an array of "arrays of strings".

You can map that to an array of so long as you have a function that takes "array of string" and turns that into "something else"

Here you map it with the Identity function, the function that just returns its input, which is going to take an array of strings as input and return the exact same array of strings as output:

Copy   raceResults.map {
       return $0 // Returns first array 
   }

This does nothing, and the result is the exact same thing as raceResults.

If you want to iterate over all these elements then the function flatMap is handy:

CopyraceResults.flatMap { $0 }.forEach { print($0) }

flatMap is flatten, then map. Flattening an array of arrays is to return an array with all the things 'flattened' one level, so [[1, 2, 3], [4, 5, 6]] -> [1, 2, 3, 4, 5, 6], but the definition of what to flatten means depends on the type of container, for example flatMap on Optional means something else to flatMap on Array.