Learn Swift by comparison (Part – 1)


Swift IDE

If you do not have access to a Mac then try these online IDE to run swift.

http://www.runswiftlang.com

swift1

http://swiftstub.com

Swift2

Both are free.

Playground

If you are using Xcode 6.x+ then you have a playground to learn swift. You can play around with swift in the playground and learn it.

Playground runs code instantly and displays the results right beside the code.

I am using Xcode Version 6.4 (6E35b)

swift3

Hello World!

Let’s write a Hello world program


import UIKit

println ("Hello World!")


import java.lang.*;
import java.io.*;


public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		System.out.println("Hello World!");
	}
}
print ("Hello World")
#include <iostream>;

using namespace std;

int main()
{
	cout << "Hello World!" << endl;
}
console.log("Hello world!");

Noticeable differences in Swift language:

  • Swift is not C-Compatible and it is not a superset of C
  • Swift does not require a main method/function
  • Swift does not require semicolon at the end of each statement

Declaring variable

import UIKit

/*
To declare a variable, use keyword "var"
using "var" is mandatory to declare variable in Swift.
*/

var address = "Dhaka"   // a string variable
var number = 20         // an integer variable
import java.lang.*;
import java.io.*;


public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String address = "Dhaka";
		int number = 20;

	}
}
address = "Dhaka"
number = 20
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string address = "Dhaka";
	int number = 20;
}
var address = "Dhaka";
var number = 20;

Noticeable differences in Swift language:

  • Swift is a strongly typed (type-safe) language.
  • Swift compiler can infers the type of the variable. If the initial value is Integer then it is an Integer variable.
  • Once a variable has been declared, it will remain to that type. You can not change the variable type after decalred.

Data types

import UIKit

/*
If you want to declare a variable without setting an initial value then you have specify the type of the variable
*/
var anIntegerVariable : Int
var aStringVariable : String
var aFloatVariable : Float
var aDoubleVariable : Double
var aCharacterVariable: Character
var aBooleanVariable: Bool

/*
Decalre and set value in one line
*/

var numberOfItems : Int = 20
import java.lang.*;
import java.io.*;


public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		int anIntgerVariable;
		String aStringVariable;
		float aFloatVariable;
		double aDoubleVariable;
		char aCharacterVariable;
		bool aBooleanVariable;

		int numberOfItems = 20;

	}
}
#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
		int anIntgerVariable;
		string aStringVariable;
		float aFloatVariable;
		double aDoubleVariable;
		char aCharacterVariable;
		bool aBooleanVariable;

		int numberOfItems = 20;
}

Noticeable differences in Swift language:

  • Variable can not be declared without default value or explicit types. Either set a value or define a type.
  • Python and Javascript does not have explicit data types
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