Thursday, July 10, 2014

Getting started with Swift

12:21 AM Posted by MAC , , , No comments
What is Swift ?
Swift is a newly launched programming language which is recently launched by Apple Inc. With launching of new programming language apple has launched very handy guide, anyone can get it for free and start learning Swift at their own. To get your free copy today click here.

Syntax
As we all knows syntax of Objective C is not as much as compare to other language. As Swift launched with their advertise Easy, Fast, Safe and Interactive.
Syntax of Swift will be more like scripting languages like JavaScript. Swift will be more friendly with web developers, if you have some experience with web development or JavaScript it will be easy for you learn this new programming language called Swift.
PlaygroundsAs name explains it self PlayGrounds which is newly feature or concept launched which replaced simulator / emulators. Now you don't needs to compile or run your code, Playground shows you real time output of your code written. As an when you writes code on Playground it shows you output instantly.  You do not need to compile it manually or nor needed to run your code.

Variable declaration on Swift
var officeNumber = 95
let floor= 4

To declare variable on Swift programming language you can use two keywords i.e. var and let

let : Used when you want to store any final for fixed value, which will not changed.
var : Used when you want to change value of variable or want to perform some manipulation later.

Most interesting thing which attracted me personally, on Swift is you can use any special character for variable declaration no specific rule to follow variable name...isn't it interesting ?

Semicolons
One more thing you may not notice about variable declaration, no need to use semicolons anywhere!
var pageNumber = 1095
var strMessage ="Hello! from Swift"

Array declaration
Array declaration on Swift is quite easy as well as easy to access.
var  fruits:String[]=["Mango", "Banana", "Orange","Apple"];
As you can see above declaration you haven't defined datatype there Swift will consider this as String. But you also define datatype while declaration as follow
var  fruits =["Mango", "Banana", "Orange","Apple"];

Of course as an when you have declared array you will need some manipulation on Array like total count, access value from particular position, set value at particular position, add new value to array remove old value from array. There process is quite easy on Swift.
var totalFruits = fruits.count // this will returns 4 

Add new item on array
fruits += "Graphs"

Get item value from particular position
var favFruit = fruits[2]

Set item value at particular position
fruits[2] ="Watermelon"
Dictionary declarationOf course how we can forget about declaration, every Objective C user's want dictionary to declare. Good news that you can also declare dictionary on Swift as well. And again it's easy to declare.

var abcdDictionary =["A":"Apple", "B":"Ball", "C":"Cat","D":"Dog"]
Key:Value pair here: A,B,C,D defines as KEY and Apple, Ball, Cat, Dog as it's corresponding values.
abcdDictionary["C"]="Cow"

Posted by: Bhavin

0 comments:

Post a Comment