Apple Swift Language Basics

By ongraph
June 1, 2015 | 1610 Views

Latest Industry insights to keep you updated on the latest happenings.

Swift language stands for three S (Simple, Strong, Safe), in comparison to Objective-C.

 

Simple:
Swift is very simple language, to understand, to code and to learn. To learn Swift, we don’t need to have knowledge of Objective-C or C. In Swift ‘type inference’ is very easy.

 

For example: in Swift,  print anything is very easy, We don’t need any class to declare, no need to call any special method, no need to import. Just use ‘println()’ method and print anything using this anywhere.

 

In Swift, declaring any member function(method) or data member(constants or variables) is very easy.

For Data members: ‘var’ for variables and ‘let’ for constants.

 

For Member functions: func <methodName>(<arguments>)  -> <return type>{<declaration>}

 

In Swift, there is no need of use of semicolon (;) at the end of any statements.

 

In Swift, there is only one file for your controller which will be viewController.swift. It does not have two files like .h or .m as in Objective-C

 

There is no need to define the type of any data member while declaring. If you are providing the value to it, Swift converts automatically the type of that data member according to the value which user has provided.

For example: var x = 10 – Swift will automatically convert x into a Int type variable.

 

In Swift we can get tuple(more than one value) from a function as return value.

 

No more square brackets([]) for messages, and alloc() and init() methods.

 

In Swift, ‘switch’ is very easy and impressive. There is no need of ‘break’ statements. In Swift, only one case will execute and then compiler will return. It will not go to the next case. ‘default’ case is necessary to declare. As switch value, we can use not only character or integer type but string too. And in as case we can pass anything like string, range, condition etc. To execute the next case, we have to use ‘fallThrough’ keyword explicitly.

 

In Swift we can use function as a return type, as an argument, inside another function as a result etc.

 

Strong:

Now in Swift, ‘null pointer’ problem is no more because if you are not providing value to any data member, you have to make it as an ‘optional’ with (!) or (?) mark. Optionals values are either nil or going to have value. If you know that this data member is definitely going have any value, so use (!) otherwise use (?) 

 

In Swift you have to use curly braces({}) to use ‘if’ statement.

 

Safe: 

Now in Swift there is no concept of pointer and references anymore. You just have to go for data members.

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin