Followers

Sunday, 7 September 2014

Classes

In the previous post we have explained how structures and classes are different from each other. Let's have a recap,

Classes and structures shares the similar definition syntax, which looks like this












What differentiate them ?

Structure: are values which are always copied when they are passed around.
Classes: use reference counting. It means more than one variable can reference a class instance.

Lets define a class with some properties, 















you might have noticed that we have done the initialization, which is not required in case of Structures. 









Inheritance: 

Inheritance feature allows a class to have a same behaviour as the parent class also behaviour can be  extended as per the need. Let's see how we can achieve this in Swift,





















Let's add 'isMorePowerful' method to our derived class 'Monitor', it will decide who is powerful among two monitor's based on the level assigned to them. 

Let's create two Monitor's with different level assigned to them, 

 Class Function:












That's it for now. I'll be updating this soon...  

Saturday, 26 July 2014

Structures

Structure & Classes are general-purpose, flexible constructs that become the building blocks of our program's code.

Syntax:













Are you confused about Structures & Classes ?

Classes use reference counting. It means more than one variable can reference a class instance whereas Structures are values which are always copied when they are passed around.

Structures 

In this tutorial we are going to do some basic operations on a very simple struct.

What all we can do with the structure ?
  • Defining properties to store values 
  • Defining methods to provide functionality
  • Defining subscripts to provide access to their values using subscript syntax
  • Defining initialisers to set up their initial state
  • Be extended to expand their functionality beyond a default implementation 
  • Conform to protocols to provide standard functionality of a certain kind     


Saturday, 12 July 2014

Functions

Functions are self-contained chunks of code that performs a specific task.

Every function in swift has a type, consisting of the function's parameter types and return type. You can,

  • return function from function 
  • function as a parameter to other function
  • Writing function with in other functions to encapsulate the useful functionality


In this post we will be working on different examples, described below
Eg. 1:
  Eg. 2:
Eg. 3:












Eg. 4:

Eg. 5:

Friday, 11 July 2014

Working with Dictionaries

What is a dictionary ?

It's a container that stores multiple values of same type. In a dictionary each 'values' is associated with a 'Key'. A 'key' in a dictionary will act as an unique identifier.

When to use dictionaries ?

Dictionaries are used when you want to lookup 'values' based on an identifier(key)


In swift the nature of the values and keys are predefined. In other words, the type of keys and values that a particular dictionary can store is always made clear, either through an explicit type annotation or through type inference.

Explicit type annotation:

In the above example we have declared a dictionary called 'dict', contains string type keys and values.

Type inference:



Lets, start working on some examples,
Eg.1

Eg.2

Sunday, 6 July 2014

Working with Array

An array stores multiple values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.
Swift array types are implemented as structures. To understand their behaviour, lets start working on some of the examples,

Understanding, How copy works in an Array:














In the above example, its clear that if you assign array 'a' to 'b' they share the same elements.
Case 1: When you change the element at index[0], they still share the same elements.
Case 2: If you append any of the array 'a' or 'b', they refer to an independent set of array elements.

Ensuring that an array is Unique:















Calling unshare() on an array, ensures a unique copy of an array.

Checking whether two arrays are Unique: In other words, making a check if they share same elements or not
Initialising, appending an array 
Iterating through an array 
Removing items from an array
Pre-Populating an array with same values, Concatenation

Saturday, 5 July 2014

Using the Switch Statement

This tutorial would give you a walkthrough of switch statement.

switch statement allows certain blocks of code to be executed depending on the value of control expression.























Let's go through the different examples,

Ex 1: Basic example 

Above mentioned e.g's are self explanatory

  1. if myTestVal is from 1-9 
  2. if myTestVal is from 1-10
  3. '_' is ignored. This case is always true  

Ex 2: Tuple 










In this example both the cases are true, so how can we make sure it continues to check if there is any other possibility,



Ex 3: Switch statement in Loop
In the above example, value is found in the very first case. What if we don't want to iterate through the whole loop once the value is found?
Check this, 
Hope this helps you.

Friday, 27 June 2014

Constants and Variables

In this tutorial we will be going through Constants and Variables. So, what does they mean in programming language?

Constant is one, for which you can not change the value during the course of a program. On the other hand,
Variables can change.      

Let's go through a basic example of above mentioned terms,

Constants are declared using the keyword let





As 'count' is declared as a constant, updating the value of 'count' is going to throw an error,

 
(include expressions)



Variables are declared using the keyword var




In this case user will be able to update the value of 'count'.