EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Swift map

Home » Software Development » Software Development Tutorials » Swift Tutorial » Swift map

Swift map

Definition of Swift map function

Swift map function comes from the background of functional programming where the map reduces and filter the function with appropriate collections like arrays and dictionaries to loop over them without using any for loop. Swift map function is also known as high-order functions as they take most of the functions as input. Input that will be provided on Swift map function needs to be analyzed properly as it will further transform the entire data into one. Once the swift map function is used for iteration then it makes the function easy to manipulate.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

let rs_collction = i_p_collection.map
{
(elemnt_of_collection) -> Rslt_type in
return transformed_data;
}

Where, rs_collction is the variable or storage variable which is used for storing the value into it with the variable to be defined into the frame cell. Followed by input map collection to be putting the value once the iteration is performed as per for loop using (elemnt_of_collection) in Rslt_type in the variable considered. At last it will return transformed_data which is the transformed data as per requirement.

How map function works in Swift?

  • Map function in swift works with variations according to the requirement and the approach that is chosen by the programmer at the time of implementation.
  • Swift programming language is a kind of language where the sequence or any ordering with the elements matter a lot with the fact that collection types that are inherited from the Collection like array, Set and dictionary matters a lot for storing values.
  • High order function is considered one of the best features of Swift programming language as it provides functions like map, sort, filter, flat map and reduce which can be used on various types of collections and sets.
  • High order function loops over a collection and allows the elements to perform the same set of operation in fact it should be exactly same without any variation.
  • There are scenarios when there is no need of all the elements rather elements should be present in the form of single set then in that case it should be ready in a way that it will remove most of the unnecessary or nil elements from the collections and the implement sequence is optional in nature. When implemented on collections or sequences the map is considered as a flattened collection.
  • Filter is a function which can be incorporated with the map function in order to provide some useful operations that allows to filter all the existing collection of elements based on requirement filtering.
  • Reduce is the function which is used for reducing of the result which gets generated from the set or arrays of elements to a single and computed value from the collection.
  • There are some closures that are applied on the sets or the collections for making transformation on the values or elements present in the transformation closure while traversal then at that time it is very much needed to keep the Swift map sync with the other operations and transformational closures.
  • Then comes variations to be applied on top of the map which are called as FlatMaps and CompactMaps all depends on the scenario of usage and requirement.
  • FlatMaps come into picture whenever the need is to make the variation on plain map to flatten and compact the result while applying with a closure will return the appropriate sequence.
  • Flat maps can take non-nil values or nil-values as well depending on the return type whether to return an optional value or no value.
  • Flat maps are also known as compact maps both are same but having difference in the naming convention due to the fact that from swift version 4.1 onwards this same flat maps naming got transformed as compact maps with very slight or minimal difference.

Examples

Let us discuss examples of Swift map.

Example #1

This program demonstrates the conversion of Celsius into Fahrenheit using swift map function which makes use of the closure to transform into the transformed set of data as shown in the output.

let clsius = [-8.0, 13.0, 14.0, 32.0, 42.2] let frhight = clsius.map { $0 * (9/5) + 32 }
print(frhight)

Output:

swift map 1

Example #2

This program demonstrates the precise and concise coding paradigm to reduce the boiler plate of implementing the map specially arr_of_int using the below format of map as shown in the output.

Popular Course in this category
Swift Training (10 Courses, 11+ Projects)10 Online Courses | 11 Hands-on Projects | 63+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,109 ratings)
Course Price

View Course

Related Courses
Windows Forms Training (4 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)

let arr_of_int = [5,2,6,2,7,8,3] let map_new_array = arr_of_int.map { $0 * 10 }
print(map_new_array);

Output:

swift map 2

Example #3

This program demonstrates the concept of iterating over the dictionary and which in turn will return the keys only from which the values will get retrieved easily as shown in the output.

let countries = [1:"Austria", 2:"India", 3: "Australia", 4: "Zimbabwe", 5: "Africa", 6: "America", 7: "USA", 8: "United_Kingdom" ] let countries_Dctnry_Keys = countries.map
{
(k_e_y, val_ue) -> Int in
return k_e_y
}
print(countries_Dctnry_Keys)

Output:

swift map 3

Example #4

This program demonstrates the concept of iterating over the dictionary and which in turn will return the values only from which the values will get retrieved easily as shown below.

let countries = [1:"Austria", 2:"India", 3: "Australia", 4: "Zimbabwe", 5: "Africa", 6: "America", 7: "USA", 8: "United_Kingdom" ] let countries_Dctnry_values = countries.map
{
(k_e_y, val_ue) -> String in
return val_ue
}
print(countries_Dctnry_values)

Output:

swift map 4

Example #5

This program demonstrates the usage of flat Map on a sequence with a closure that returns a sequence of values.

let rslt_mrks = [[2,3,5], [6,8], [10,18,16]] let totl_rslt_marks = rslt_mrks.flatMap { $0 }
print(totl_rslt_marks)
let passng_marks = rslt_mrks.flatMap { $0.filter { $0 > 3} }
print(passng_marks)

Output:

swift map 5

Example #6

This program demonstrates the reduce function with a concatenate function that can be applied incorporation with the map function in a similar manner that will print the following output as shown below.

let cds = ["jkl","mno","pqrs"] let txt_0 = cds.reduce("", +)
print(cds)
print(txt_0)

Output:

swift map 6

Conclusion

Swift map is the type of function which have a lot of benefits associated with it. It helps in overcoming the complex challenges with for loop by making the iteration property simpler with swift map function. It makes the overall language quite flexible, optimized, and easy to understand while implementation. Swift map has improvised the entire traversal and organization property of array, dictionary, and sets.

Recommended Articles

This is a guide to Swift map. Here we discuss the Definition, How map function works in Swift, and examples with code implementation. You can also go through our other suggested articles to learn more –

  1. What is Swift?
  2. Swift Version
  3. Swift For Loop
  4. Swift Operators

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Swift Tutorial
  • Basics
    • How To Install Swift?
    • Swift Version
    • Swift Operators
    • Swift For Loop
    • Swift Dictionary
    • Swift forms
    • Swift map
    • Swift Interview Questions

Related Courses

Swift Training Course

Windows Forms Training Course

Kali Linux Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Swift Training Course Learn More