Getting Started with SWIFT

By ongraph
August 8, 2014 | 1910 Views

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

In the WWDC keynote, i was amazed to see a innovative feature Playgrounds that allow us to experiment started with Swift and see the results in real time on the left hand side of our code. The best thing about playgrounds is as you type code there, you’ll see the results immediately without the overheads of compilation.
 At the time of writing this, like many other developers, I’m new to Swift. I have downloaded the Apple’s Free Swift Book and played with playground and Swift
NOTE : At the time of writing this tutorial, Xcode is still in beta mode so we cannot post screenshots of it.
Features of Swift :-
1. Modern :- 
As a result of research on programming languages, combined with years of programming in Apple platform developments, Swift evolved as Modern language in which API’s are easier to read and maintain.
Inferred type make Swift cleaner, Memory is managed automatically, generics, Fast and concise are the other features that Swift offers.
2. Interactive Playgrounds :- 
As i have already given some introduction about Playgrounds in Swift, they make Swift incredibly simple. its like getting immediate response from someone without much delay or i should say seeing the output of code without you clicking that run button in Xcode. Type a line of code and result appears immediately. if you are running a loop then you can see its progress on each step. With Playgrounds you can draw a line, create an animated scene using SpriteKit.
3. Run-Eval-Print-Loop (REPL) :
Yes, thats what REPL stands for Run-Eval-Print-Loop. it is the debugging console in Xcode that includes a version of Swift built in it. Use Swift syntax to evaluate and interact with your running app, or write new code to see how it works in a script like environment.
4. Designed For Safety : 
In Swift the possibility for unsafe code is removed. Variables are initialised before use and memory is managed automatically. Adding a single line of code in can replace what used to be an entire line of code in Objective-C.
5. Fast and Powerful :- 
Swift is built to be fast. Using the high performance LLVM compiler, Swift is converted into Optimised native code. The syntax have been tuned to get the most out of it. it also provides Object-Oreinted Features.
You can start developing apps with Swift immediately or you can enhance existing ones. New Swift code can coexist with Objective-C code in same project.
To get Started with Swift, download Xcode 6 beta and be sure to study the release notes
Comparison with Objective C: 
Similarities to Objective-C
  • Basic numeric types (Int, UInt, Float, Double)
  • Most C operators are carried over to Swift, but there are some new operators
  • Curly braces are used to group statements.
  • Variables are assigned using an equals sign, but compared using two consecutive equals signs. A new identity operator, ===, is provided to check if two data elements refer to the same object.
  • Square brackets are used with arrays, both to declare them and to get a value at a given index in one of them.
  • Control statements, for, while, if, switch are similar, but have extended functionality, e.g. a for in that iterates over any collection type, a switch that takes non-integer cases, etc.
  • Class methods are inherited, just like instance methods; self in class methods is the class the method was called on.

Differences from Objective-C

  • Statements do not need to end with a semicolon (‘;’), though they must be used to allow more than one statement on a line
  • Header files are not required
  • Strong typing
  • Type inference
  • Generic programming
  • Functions are first-class objects.
  • Enumeration cases can have associated data.
  • Operators can be redefined for classes, and new operators can be created.
  • Strings fully support Unicode. Most Unicode characters can be used in either identifiers or operators.
  • No exception handling.
  • Several notoriously error-prone behaviors of C-family languages have been changed:
    • Pointers are not exposed by default. There is no need for the programmer to keep track of and mark names for referencing or de-referencing.
    • No need to use break statements in switch blocks. Individual cases do not fall through to the next case unless the fallthrough statement is used.
    • Variables and constants are always initialized and array bounds are always checked.
At last, a question that is coming to every developer’s mind right now is Should I Stick to Objective-C or Migrate to Swift?.
From my point of view, Swift is the future of programming for Apple platforms. Apple will continue to add features to the language, as you see the syntax is more developer friendly and easy to learn for a beginner.  Though Objective-C will not disappear overnight and is here to stay for years because there are tons of code libraries written in Objective- C, it will take time for developers to convert that to Swift.
(a) If you are in iOS development full time – It will be best if you learn both. this is because there is existing code in Objective c that you need to understand. As i stated earlier there is tons of libraries written in Objective-C that you will use. Things will be transitioning to Swift over time.
(b) if you are a newbie – If you intend to use only Swift from the start, you will get knowledge of it only by using it. But if you have time, its still a good idea to learn Objective-C so that you can make use of existing libraries in Objective-C.
References :

Monthly industry insights to keep you updated on latest happenings

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