Thursday, July 10, 2014

Hello Word with Swift

7:13 AM Posted by MAC , No comments
Hello Word! with Swift programming language

Today we will gonna learn how to create first Hello Word! program with Swift. Hope you have already gone through our previous post. If not I suggest you to go though first post that will help you to get basic idea about Swift programming language.

To run any Swift program you will need following xCode 6 (beta), as we have discussed in previous post Apple have launched new concept called Playground. So no need to compile and no need to run code. It will show you real time output of code. Steps to create first Hello Word! Program with Swift
Step 1: Open xCode 6- Click on Get started with a playground



open xcode 6 and click get started with a playground
Step2 : Save your first Playground on hard disk HelloWord.playground



Step3: Now you will have first playground in front of you.


As of now you have followed steps 1, 2, 3 you will have your first swift program. Right now it will have default text called "Hello, playground".

You will see there is only variable declaration named str still you can see output there. It displays current values of that variable. From out previous post you can borrow some code any try play with here on this playground. Below is some example code which will help you to do some practice with your first HelloWord.playground
Second very basic program with swift
Here is blow some code where you will be learn
1) How to create class
2) How create variables in class
3) How to initialise class
4) How to create user define function in class
5) How to create object and initialise?
6) How to call class methods using object

1) How to create class ?
class Maths{

}

2) How create variables in class ?
let total: Double
let valOne:Doublelet valTwo:Double

3) How to initialise class ?
init(valOne:Double,valTwo:Double){
        self.valOne=valOne;
        self.valTwo=valTwo;
        total=valOne+valTwo;
}
4) How to create user define function in class ?
func printTotal(){
        println("Total \(total)")
}
5) How to create object and initialise ?
let objMaths = Maths(valOne: 22.25,valTwo: 30.45);

6) How to call class methods using object ?
objMaths.printTotal();

Posted by: Bhavin

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