Swift 3 : Basic concepts with Xcode 8

Swift3 at WWDC

Apple has announced Swift3 on WWDC [2016] this year. It’s amazing that Swift can be used on Linux and lots of third party companies working on server side swift. First couple of releases of Swift has gone though major changes in the Swift but Apple has promised that Swift 3 will be stale release and can be adopted by companies without any fear. Apple has also provided migrator for the Swift2 to Swift3 which makes migration easy.  In this tutorial, we will learn basics of Swift announced at the WWDC ‘Getting Started with Swift” talk. I would strongly recommend watching this talk.

Let’s look at what’s new in the swift with basic concepts. Please note this isn’t tutorial to learn all Swift but to get brief overview of what’s inside the Swift.

Pre-requisite

  • Swift 3 SNAPSHOT Or
  • macOS Sierra and Xcode 8
  • Docker Image for Swift3: IBM-Swift sounds promising. Get Docker image here 

Swift3 Basics

Swift 3 has some new features in the language which adds type safety while writing code. Let’s look at some basics of the language

Variables and Constants

 

In Swift, variables are declared as ‘var’ and constants are declared as ‘let’. We can determine what is going to change and what isn’t, we can assign the variable and constant accordingly. e.g Swift version will be changing so we can declare it as ‘var’ but language name , year introduced will not change.

varibales_constant

Array and Dictionaries

 

Arrays and dictionaries are collection data types in Swift. Arrays are pretty much similar to other languages like Ruby, PHP and Dictionaries is same as Hash in Ruby or Associative array in PHP  which has   key and values. Similar to Ruby code we can iterate over to the Array and Dictionaries using ‘for..in’ loop.

We can also modify and access elements using indices and Key. Below is the example of how to iterate over array of Strings.

array_dictonary

 

Loops and Control Flow

 

Swift3 doesn’t have  C Style for loops. It only has ‘for….in’, ‘ repeat….while’ and ‘while_loop’ . Like other languages  Swift also has ‘if…else’, ‘Switch’ control flows. Below are the example of some loops and control flows.

 

loops

 

Functions

 

Functions perform some task and may and may not return some value as we might seen in Ruby or PHP function takes some argument and return something. In Swift function can take arguments, functions, closures etc etc and returns multiple values which is called ‘tuple’.

In order to make function call more readable Swift has made function argument more flexible as we can add extra parameter or completely omit parameter  using ‘_’. We can see how readable the function call in the below function

function

 Closures

 

Closures are nameless functions or block of code that can be passed around. In Ruby, it has similar concept of blocks or lambda. In Swift, Closure is usually written in “{” with parameter types/return type and body is separated by word ‘in’.

Below is the example of Generic function which adds even numbers to results array from input array. We have passed closure to the function.

closures

Like Ruby we can filter and map arrays, dictionaries using closure passed to it.

map_closures

 

Structs

 

In Swift, Struct can take properties, functions or computed properties. it does’s most of the things like classes but not all. Structs can be instanciated to access properties and functions from structs.

structures

Classes

 

There is no significant difference in Swift in the way classes can be used in other languages. Class has properties, initialiser, functions. It can be sub-classed and value can be passed to children classes. We can override values from parent class in the child class etc etc

classes

Generics

 

Generics are quite useful concept to avoid duplication of functions, structures, enums for different data types. e.g we want function to take array of integer and return a integer Or we want array of String to return String. We don’t need to write two functions i.e one for Int and one for String. We can use generics instead.  Below is an example of Struct for array elements ..

generic_structs

 

Enums

 

An Enumération is special case in Swift which allow us to store different cases in nice way and access the values of those. We can access enum using name.key and values like name.key.rawValue . Below is simple enum to see Rank in the deck of cards.

 

enum

 

Protocol and Extension

 

Protocols are like Interfaces from Java. It defines set of rules but won’t allow function body in it. All other data types like ‘enums’, ‘Structs’ and Classes can confirm to the any suitable  protocol. This concept will change how we used to programme software. Please watch this video from WWDC 2015 ‘Protocol Oriented Programming in iOS

 

protocol_extension

That’s it for now. I will post more about Swift in the near future.

As I mentioned  earlier, this is not end to tutorial. just writing to get basic skeleton of Swift.

Source-Code

You can find sample playground on Github Here  

https://github.com/Shashikant86/Swift3-WWDC