Tuesday, 30 June 2015

Data Types In Swift

Data Types In Swift

As i described in my last tutorial how to setup environment for windows as well as mac users.
If you have not studied click here:Environment Setup in Swift

Learn Objective C from Here:Click Here

What are the data types?

I describe about data types while studying objective c.
Data Types in Objective C: click here


Data Types describe the nature of data stored in memory addresses.
Developers has provided some memory to run application.
If they stored data according to their types then memory is proper ally maintained.
You have Integer Value which is maintained at 8bit to 32bit according to their types short and long integer. If you use string data type for integer value storage that is not the good way.

Generally in Swift support all data types.

Integer
Float
Double Float
Bool 
Character
String



How to Write Variables in Swift?

Swift is very fast language and safe language. Its compiler is very smart. You do not need to write  data types with swift like javascript. Swift compiler automatically inferred it for you.
If you declare variable in swift you need to write only

var x=90

If you declare in objective c  you need to write the 

int x=90;

Swift compiler automatically convert the type to integer.

Same case for String and any other type;

var print="Hello World"


For objective c you need to write Pointer like this:

NSString *print=@"Hello World";

Even you do not need to write semicolon in writing at end of statements.Also do not need to write @ sign each time writing Strings. As i told you swift compiler is such a smarter compiler.

For float

var a=9.78


In case of objective c

Double Float a=9.78;

You can also specify the data types with swift if you have free time. But if compiler anything do for you why to write It.

Like below statements is also legal in swift

var q:int16=49

In general, Swift is loosely Typed language.


How to print statements at console in Swift?

You need to write only println("Some statements")

Hello World Example in swift:

var name="suraj"
println("Your name is: \(name)")


In Objective c

NSString *name=@"suraj";
NSLog(@"The name is:%@",name);


Which one is more easy and readable.
Obiously Swift has easy syntax.

You think how compiler  behave when first you initialised with integer and then string.


Like var q=90

q="Suraj'

It generates error. It only takes data types of first initialisation.

So q will be only Integer.

So it is one of the safe language.Its compiler generates error and work Safer.

You can change initialisation while inferring types to data type.
With keyword typeAliases

typeAliases Name=String

Var p:Name="Suraj"

println(p)

Swift is very fast language you can see output side by side.
For learning and testing purposes apple provide the Playground features to run apps just like below:







Create Table View in IOS With Objective C : Click here

I hope you like my tutorials. If you have any query can mail me here and comment here.


My Next Tutorial will be Methods,Classes,Objects.

No comments:

Post a Comment