A brief guide to Swift, Apple’s new programming language for app creators

Wed 09 July 2014 Written by Evi
Evi

Apple announced their new language Swift at the WWDC 2014. For the application developers of you out there, this means you’ll need to get familiar with Swift, so we’ve put together a quick “cheat sheet” on the things you need to know most:

First though, a few words about Swift.

Swift, an Apple developed programming language, has been designed to work with existing code bases in a safe manner. It does, however, have a different approach to coding for Mac and iOS devices. It works with Xcode 6. It will be interoperable with Objective-C but there’s an enhanced instruction set with Swift. It aims to rid developers of the problems of the C-language legacy era - you’ll no longer need to manage memory to the same degree and that means it’s safer than Objective-C.

You may find that you’re familiar with some of the development paradigms if you use Python, C#, Ruby.

Can I use Swift now?

Yes, there’s a beta release on the Apple developer’s site. It’s pretty easy to install and get working with.

What improvements will I find in Swift?

App developers will be pleased to learn that language readability has been the number one priority for the launch release.

Control flow

Switch statements are different in Swift. You can match against a whole new set of elements (like enums or Boolean expressions, for example) and that means you’re less likely to find that your code fails easily.

Optionals

You can now shift between different types without having to fall back on null checking. This is achieved by the use of optional types in Swift. You can even string optionals together to offer additional protection against exceptions.

Type inference

You can stop annotating your variables with the type information. The Swift compiler is able to infer the type by examining the value of the variable.

Type safety

Welcome to C++ style debugging. When the compiler can infer the type, it can make a much better judgment call regarding the source of the error. That makes it much easier to locate the problem in the code and fix it.

Strings

You can now concatenate string using “+=” and that offers much cleaner, easier to follow code.

Speed of execution

Swift compiled code is fast. Apple are claiming that it’s possibly 75% quicker than Objective-C code. That means a greater degree of flexibility in applications that handle heavy graphics.

Easier to Learn

Objective-C is a pain for new developers to learn, it’s too darn complex. Swift’s simplified syntax will make it more accessible to novices. It’s still not the easiest language out there but it’s much better than before. It’s also easier to use for experienced programmers - simpler syntax is a benefit to everyone.

Bug Number Reduction

Application developers will find it easier to find and fix errors in code and that means better, more stable apps at release.

Playgrounds

This new feature lets you visually test code without compiling it. You can see if something works right without any hassle.

Summary

My recommendation: consider downloading a copy of Swift and start getting comfortable with it.  Swift needs to mature and prove itself in the real world of app development. Yet there’s no doubt that swift is a step forward when compared to objective – c hence iOS engineers should gradually give it more attention and eventually adopt it.