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

Playground

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

Variables

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

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

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

func eatBurger(){
}

and called using the function name with the paranthesis

eatBurger()

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
}

Loops

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
println()
}

So the x will loop from 1 to 100

Example 2

var name  = "cat"

for y in name{
println(y)
}

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

c
a
t

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

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

Range

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 {
println(x)
}

this will print 1 to 100

similarly

for y in 1..<100{
println(y)
}

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
default:
// 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
default:
break

}

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s