Add



Swift Language Basics

Swift is a new programming language for IOS development. Swift is an object oriented programming language. Like other languages, it has the concept of classes, inheritance, structures, and enums etc. You can define Protocols in it like you define Interface in other languages.

Writing to Console Output

To write on console output, print method is used.

    print("Welcome to Swift Learning at omerjaved.com")

Semicolons

In swift, you do not write semicolons at the end of the statement.

    print("Hello, Omer!")
    print("How are you doing?")

However, if you want to write multiple statements in one line then you have to separate them with the semicolon.

    print("Hello, Omer!"); print("How are you doing?")

Constant and Variable

To declare a constant, let keyword is used.

For example,

    let myName : String = "Omer Javed"

To declare a variable, var keyword is used. To give it initial value, equal sign it used.

    var location : String = "France"
    location = "Italy"

Constant value cannot be changed once a value is assigned to it. Variable value can be changed later like in the example; "location" variable was first set to "France" and then set to "Italy".

Array

Array is used to store same type values. Index starts at 0. You can declare array using starting and ending brackets i.e. []

    var  places = ["New York", "Paris","London"]

or

    var  places : [String] = ["New York", "Paris", "London"]

To initialize empty array:

    var allPrimes = [Int]()

To insert new Item in array, array’s insert method is used.

    places.insert("California", at:0)

"at" parameter is for the position where to add new item.

To remove an item from a particular position, array’s remove method is used.

    let place = places.remove(at: 1)

To empty an already filled array, we assign "[]" to the the array variable.

    places = []

There are 2 ways to iterate over an array:

    for place in places 
    { 
        print(place) 
    }

To get index and value both, you have to use enumerated() method of array.

    for (index, value) in places.enumerated() 
    { 
        print("Place \(index + 1): \(value)") 
    }

Sets

A set stores distinct values of the same type with no defined ordering.

    var allIntegersSet : Set = [1,7,9,34,15,42] 

There is intersection(_:) method to create a new set with only the values common to both sets. There is symmetricDifference(_:) method to create a new set with values in either set, but not both. There is union(_:) method to create a new set with all of the values in both sets. There is subtracting(_:) method to create a new set with values not in the specified set.

To loop through a set, for-in loop is used:

    for number in allIntegersSet
    {
	    print(“\(number)”)
    }

To insert an item in the Set, insert method of Set is used. For example,

    allIntegersSet.insert(17)

To remove an item from the Set, remove method of set is used. For example,

    allIntegersSet.remove(9)

Dictionaries

In dictionary, data is stored in the form of Key-Value form.

    var socialMediaNames = [String,String]()
    socialMediaNames["fb"] = "Facebook"
    socialMediaNames["t"] = "Twitter"
    socialMediaNames["p"] = "Pinterest"

To print the count of dictionary items, count property is used.

    print(“This dictionary contains \(socialMediaNames.count) items”)

To loop through the dictionary, for-in loop is used:

    for (socialMediaCode, socialMediaFullName) in socialMediaNames
    {
    	print("\(socialMediaCode): \( socialMediaFullName)")
    }

To remove an item from dictionary, removeValue method is used.

    socialMediaNames.removeValue(forKey: " fb")

There is another way to remove an item by assigning nil to item:

    socialMediaNames ["p"] = nil

Now item of dictionary socialMediaNames, which has key "p" will be removed.