Tag Archives: basic

Basic Swift for iOS: Core Syntax

Hi guys, I was given a Mac Book Pro recently and thought of learning some iOS development. So as a first step thought of going through the Basics first. And as you now sharing what I learn with other is the number one way of learning…! This blog will go in a format of short notes or points.
So here you go guys…!

Most of the basics are same as most other popular languages, but still there are some changes, Ill be high lighting the serous issues.

We don’t use semi colones at the end of the statements in Swift.
Also we avoid parenthesis when declaring a condition as much as possible, if there is a complex logic its advised you use it, other than that nope.

We follow the camel case naming convention in Swift


There is a tool called Play Ground in Xcode where you can write some code and observe the results soon as you write them. Its a great tool to learn the basics and to test some complex logics


In swift you don’t have to mention the type when you are creating a variable, it automatically decides itself according to the values you pass.

var myInt = 32
var myString = "cat"

But if you want to create a variable without assigning a value you can specify it like this

var myVariable :String
var myIntVariable :Int


Constants are variables with immutable values, so once a value is defined that value cannot be changed after that

So they are declared with the word “let”

let myConstVariable = 32
let myUnChangedVal = "car"

String Interpolation
Ooops don’t get scared by the word. Its basically a way of passing values or concatenating values in between strings.

var name = "Batzee"
var level = 11

just say you got 2 String values which u have to add in between a standard println() statement

println("\(name) is a Level \(level)" Agent in Ingress")

So the out put will be

"Batzee is a Level 11 Agent in Ingress"

So here instead of using ‘+’ operator we are using ‘\()’ operator


Functions in Swift are created with the key word ‘func’

func eatBurger(){

and called using the function name with the paranthesis


If the function has a return type then the function is written as

func eatBurger() -> Int{
var burgerNo = 3
return burgerNo

‘->’ followed by the return type must follow the function before the curly braces starts.

if Statements

if statement is same as you have written an ‘if’ statement anywhere else, but there are certain things you have to consider when writing in Swift.
As always the Swift says it’s best practice if you can avoid the parenthesis around the condition
And on statements that has one line of executable line of code on success also must have it self surrounded by the curly braces unlike other languages, where you can avoid using the curly braces around the single line code.

if  x<500  {
return x


So there are 4 kinds of loops in Swift

The 1st one is the traditional for loop there is not much difference, only thing you have to consider is to remove the parenthesis around the condition

for  var x = 0; x<10 ; x++  {


The 2nd one is the for in loop this is something like foreach loop you specify a collection to loop through. Whatever you pass it converts it in to a collection. If you give a range, it loops through the range. If you pass an Array, it loops through the array element, and if you pass a string it converts the String in to character array and loops through the character array elements

Example 1

var total = 0

for x in 1...100{
total = total+x

So the x will loop from 1 to 100

Example 2

var name  = "cat"

for y in name{

here the variable name will be taken as a string array and the out put will be


The 3rd one is the While loop, it is same as any other while loops in other languages, only considerable change is, not necessarily to add the parenthesis around the condition

while condition {
// do if the condition is true

The 4th one is the Do While loop, in this there is some noticeable change, the condition is added at the end of the body, this is to run the code inside the body at least once before it checks on the condition

//runs for the 1st time and runs for the second time and after, if the condition is satisfied
}while condition


Range operator is something new in Swift

which is defined by ‘…’ this says to consider the number mentioned in the left of the operator and all the numbers in between it and the number mentioned in the right of the operator.
Also in the Range operator you can control the 1st and the last dot, like ‘..<‘ , ‘>..’ so this basically will work like a for loop

for  x in 1...100 {

this will print 1 to 100


for y in 1..<100{

will print 1 to 99

Switch Cases

So the Switch Statement in Swift has some points to consider .

The case range must be Exhaustive, unlike other languages you cant check only for a handful of conditions but for all the possible values.
for example if you are checking for integer cases, integer is never ending so u have to handle it. In that case you will use the default case to catch all the situations that you don’t want than what u actually wanted.

let chocoCount = 7
Switch chocoCount{

case 0:
//in case 0 do this
case 1:
// in case 1 do this
case 12:
//in case 12 do this
// do this if the cases dont match the given cases


Also in Swift’s Switch statements they don’t have the ‘fall through’ option, which automatically falls into another case for a range of cases. In that case we can use the range case, We have already seen what is a range operator does above.
This is how it is done.

switch chocCount{

case 1...7:
//if the case is between 1 and 7 do this
case 8...15:
// if the case is between 8 and 15 do this


This post is written after learning from a video, so there are some chances that I may have made some mistakes, so please feel free to comment them down, Ill me more happy to correct them. And stay tuned in for more blogs on Swift and iOS development.

Reference: Notes are taken by studying the Swift Essentials from Lynda.com


Some Basic Java Script

Hi guys, I thought of learning some Java Script as it has become one of the most important aspect of a programer in the present. So I thought of handling some basic default functions for a start. Hope you guys achieve some thing out from this post.

So here we will see how to write JavaScript inside the Basic html inside Body tag. We will see the external Java Script in another Tutorial.

So here we define the Script Tag inside the body and Write all the code explained between the Script Tags.

noscript tag is attached to inform people who are viewing this site in a browser that does not support Java Script.

<html xmlns="http://www.w3.org/1999/xhtml">

<script language="javascript" type="text/javascript">

//Java Scrip Code Goes Here

<noscript><h3>This site needs Javascript Enabled Browser</h3></noscript>


So below are some basic Java Script code you can try yourself

1. A simple Alert

alert("Hi this is amalan");

2. Min and Max number supported by JS

document.write(Number.MAX_VALUE + "<br/>");
document.write(Number.MIN_VALUE + "<br/>");

3. Printing a String

var sampl_str = "Here are some Escape charecters \" \t \' \\ \n";
document.write(sampl_str + "</br>");

4. Combining Strings

var first_String = "First String";
var second_String = "Second String";
var combined_String = first_String + second_String ;
document.write(combined_String + "</br>");

5. Example of using substring function

document.write("Sub String " + combined_String.substring(12, 18) + "</br>");

6. Getting the Last character

document.write("Last Charecter =" + combined_String.charAt(combined_String.length-1) + "</br>");

7.Usage of Indexof function

document.write("Index of " + combined_String.indexOf('t') + "</br>");


var str_var = "5";
var num_var = 10;

var total = num_var + str_var;
var multy = num_var * str_var;

document.write("Total is :" + total+"</br>");
document.write("Multy is :" + multy+"</br>");

9.Force data type conversion

total = num_var + Number(str_var);
document.write("Total is :" + total+"</br>");

total = num_var + num_var;
document.write("Total is :" + total+"</br>");

total = num_var + String(num_var);
document.write("Total is :" + total+"</br>");

10. Float

var float_var = 3.1417376464646464;
var float_str = float_var.toFixed(7);
document.write("Shortened PI value by 7 : "+float_str+"</br>");

document.write("Data type of float_var = "+typeof(float_var)+"</br>");
document.write("Data type of float_str = " + typeof(float_str) + "</br>");


var bool_var = Boolean(23);
document.write("Booloean of 23 is " + bool_var + "</br>");

12. Compare Variables

var rand_num1 = 5;
var rand_num2 = 10;

document.write("5 > 10 = "+(rand_num1 > rand_num2)+"</br>");
document.write("5 >= 10 = "+(rand_num1 >= rand_num2)+"</br>");
document.write("5 == 10 = "+(rand_num1 == rand_num2)+"</br>");
document.write("5 <= 10 = "+(rand_num1 <= rand_num2)+"</br>");
document.write("5 < 10 = "+(rand_num1 < rand_num2)+"</br>");
document.write("5 != 10 = "+(rand_num1 != rand_num2)+"</br>");

13.Logical operators

document.write("Is 5 < 10 and 5 != 10 "+((rand_num1 < rand_num2) && (rand_num1 != rand_num2))+"</br>");
document.write("Is 5 > 10 or 5 != 10 "+((rand_num1 > rand_num2) || (rand_num1 != rand_num2))+"</br>");
document.write("Is 5 < 10"+ !(rand_num1 < rand_num2)+"</br>");



var vehihcals = new Array("Car", "Van","Bus");
document.write("Second Value of the Vehical Array is :" + vehihcals[1]+"</br>");

15.Cycle through Array

for (i in vehihcals) {

document.write("vehical value "+ (Number(i)+1)+" is "+vehihcals[i]+"</br>");


16.Conditional Opperators

var rand_var = (5 < 10) ? "5 is less than 10" : "5 is greater than 10";
document.write(rand_var + "</br>");

17.if Else

if (5 > 5) {
document.write("5 is greater than 10"+"</br>");

else if (5 < 5) {
document.write("5 is less than 10"+"</br>");
else {
document.write("Both are Equal"+"</br>");

18.Switch Case

var state = "Colombo";

switch (state) {
case "Kandy":
document.write("Colder than Me" + "</br>");

case "Colombo":
document.write("Same as Me" + "</br>");

document.write("I dont know what you have entered"+"</br>");


var count = 1;

while (count <= 10) {

20.Continue Statement

var count = 1;

while (count <= 10) {

//try break
//if (count == 6) {
//    break;

if (count % 2) {

document.write(count + "</br>");


var count = 10;

do {
document.write(count + "</br>");
} while (count > 10);


for (var count = 0; count <= 10; count++) {


Get the Code here : Download code

Reference : YouTube Reference