Followers

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.