
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to Convert Set of String to Array of String
In this article, we will learn how to write a swift program to convert set of string to array of string.
An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. Now to convert a set of string to an array of string we use the following two methods −
Using Array() initializer
Using map() function
Method 1: Using Array() Initializer
To convert a set of string to an array of string we can use Array() initializer. It returns an array created from the given sequence.
Syntax
Array(InputValue)
Here, InputValue is a sequence which we want to convert into an array.
Algorithm
Step 1 − Create a set of string.
Step 2 − Convert the set of string into array of string using Array() initializer
Step 3 − Find the type of the resultant variable.
Step 4 − Print the output.
Example
Following Swift program to convert set of string to array of string.
import Foundation import Glibc // Creating a set of string let strSet: Set = ["Cow", "Cat", "Dog", "Bee", "Bird","Ants"] print("Set:", strSet) print("Type:", type(of: strSet)) // Converting set of string into set of an array var strArray = Array(strSet) print("\nArray:", strArray) print("Type:", type(of: strArray))
Output
Set: ["Cow", "Cat", "Dog", "Ants", "Bee", "Bird"] Type: Set<String> Array: ["Cow", "Cat", "Dog", "Ants", "Bee", "Bird"] Type: Array<String>
Here in the above code, we have a set of string. Now to convert set into array we pass the given set to the Array() initializer. Then this initialiser return an array of string.
Method 2: Using map() function
The can use map() function to convert a given set of string to an arr ay of string. The map() function is used to get an array which contains the result fo mapping the given closure over the given collections s elements.
Algorithm
Step 1 − Create a set of string.
Step 2 − Convert the set of string into array of string using map() function.
Step 3 − Find the type of the resultant variable.
Step 4 − Print the output.
Example 1
Following Swift program to convert set of string to array of string.
import Foundation import Glibc // Creating a set of string let strSet: Set<String> = ["Toffee","Candy","Chocolate","Bites","Lollipop"] print("Set:", strSet) print("Type:", type(of: strSet)) // Converting set of string into set of an array var strArray = strSet.map{return $0} print("\nArray:", strArray) print("Type:", type(of: strArray))
Output
Set: ["Candy", "Toffee", "Lollipop", "Chocolate", "Bites"] Type: Set<String> Array: ["Candy", "Toffee", "Lollipop", "Chocolate", "Bites"] Type: Array<String>
Here in the above code, we have a set of string. Now we convert the set of string into the array of string using map() function. Here this map() function return a string from the set starting from index 0 and store the result into a variable in the form of an array.
Example 2
Following Swift program to convert set of string to array of string.
import Foundation import Glibc // Creating a set of string let strSet: Set<String> = ["Blue","Yellow","Pink","Black","Orange","Red","Green"] print("Set:", strSet) print("Type:", type(of: strSet)) // Converting set of string into set of an array // in sorted form var strArray = strSet.sorted().map{return $0} print("\nArray:", strArray) print("Type:",type(of: strArray))
Output
Set: ["Orange", "Green", "Black", "Blue", "Yellow", "Pink", "Red"] Type: Set<String> Array: ["Black", "Blue", "Green", "Orange", "Pink", "Red", "Yellow"] Type: Array<String>
Here in the above code, we have a set of string. Now we convert the set of string into the sorted array of string. So to do this first we use map() function which will return strings from the set starting from index 0 and then we sort them in alphabetical order using sorted() function and finally store the result into a variable in the form of an array.
Conclusion
So this is how we can convert a set of string into an array of string. Here, the order of the elements of the resultant array may not be same as the original set because set does not have a defined order for their elements.
- Related Articles
- Swift Program to Convert an Set of String to Comma Separated String
- Haskell Program to Convert Set of String to Array of String
- C++ Program to Convert Set of String to Array of String
- Golang Program To Convert Set Of String To Array Of String
- Swift program to convert string to an array
- Swift program to convert array to a string
- Convert Set of String to Array of String in Java
- Swift Program to Convert Array to Set
- Swift Program to Convert Set into Array
- Program to convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Swift Program to Convert a String to Lowercase
- Swift Program to Convert a String to Uppercase
- Swift program to convert boolean variable into string
- Swift Program to convert int type variable to string
