DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

Day 2 : 100DaysOfSwift🚀

Day 2 :

arrays, sets, Tuples, dictionaries and enums

1.Swift’s strings, integers, Booleans, and Doubles allow us to temporarily store single values, but if you want to store many values you will often use arrays instead.

An array stores values of the same type in an ordered list.

var Names: [String] = ["Saurabh","Sachin"]
Enter fullscreen mode Exit fullscreen mode

Array position start with 0,so if you want to read sachin then

Name[1]
Enter fullscreen mode Exit fullscreen mode
  1. Sets are unordered collections of unique values.
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]

or 

var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]


Enter fullscreen mode Exit fullscreen mode

If you try to insert a duplicate item into a set, the duplicates get ignored.

var favoriteGenres: Set = ["Rock", "Classical", "Hip hop","Rock","Classical"]
Enter fullscreen mode Exit fullscreen mode

You can create set from Array

var Names= set(["Saurabh","Sachin"])
Enter fullscreen mode Exit fullscreen mode

3.Tuples are created by placing multiple items into parentheses, like this:

var name = (first: "Taylor", last: "Swift")

Enter fullscreen mode Exit fullscreen mode

access item from tuple

name.0
or
name.first
Enter fullscreen mode Exit fullscreen mode

Remember, you can change the values inside a tuple after you create it, but not the types of values.So, if you tried to change name to be (first: "Justin", age: 25) you would get an error.

4.A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
Enter fullscreen mode Exit fullscreen mode

5.According to the Swift documentation enumeration is defined as “a common type for a group of related values and enables you to work with those values in a type-safe way within your code”.
for more details about enum Click Here..

6.Creating empty collections
Arrays, sets, and dictionaries are called collections, because they collect values together in one place.
Empty Dictionary

var teams = [String: String]()
Enter fullscreen mode Exit fullscreen mode

we can add

teams["Paul"] = "Red"
Enter fullscreen mode Exit fullscreen mode

empty Array

var results = [Int]()
Enter fullscreen mode Exit fullscreen mode

empty set

var words = Set<String>()
var numbers = Set<Int>()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)