Introduction to Swift: an Apple’s revolution for all developers

Apple released Swift in WWDC 2014. A new, fresh and safe language that aims to be productive and maintainable. A language that will actually make you want to code.

swift apple

In 2014 there were a lot of projects, startups and big companies that needed to be in every smartphone in the world. By that time, we were already able to do everything from ordering a cab to booking a hotel just by opening an app. We went to the movies, exchanged money and talked to our family in the other side of the world from the comfort of our couch.

That was amazing for the final user and still is these days, but for the people developing that apps, it was not so wonderful. At least, every app needs to run on iOS and Android, which meant people writing in Objective-C and Java. The fact that an app for the futuristic today we are talking about needs those old languages with all their problems is really annoying, taking into account that we already have wonderful tools like JavaScript or Angular.js.

There comes Apple, releasing Swift in WWDC 2014. A new, fresh and safe language. A language that aims to be productive and maintainable. A language that will actually make you want to code.

But why? Why would I move to Swift?

Swift has a fluid syntax that makes it easy to read. Without a doubt, it was inspired in Python and Ruby. Objective-C was built on C, so it has the heritage of an old language intended for low level programming. Swift was not build on C, so it can drop all unnecessary keywords and syntax elements that are difficult to read.

Apart from that, Swift pointers are better handled. In Objective-C, if you try to call a method of a nil (also known as null in other languages) pointer, it just does not do anything. This was intended to prevent the application from crashing. However, this also caused numerous bugs since it leads to unpredictable behaviour. Additionally, the developer must take care of documenting the code if a certain return value can be nil, using comments and naming conventions for methods.

To solve these problems, Swift introduces Optional types, which you may know if you have worked with Java 8 or .NET. This type explicitly allows nil to be a value. Since it can be used in the signature of a method as the return type, the developer that calls this method will know upfront that he may expect a nil value. Besides, Swift raises a runtime exception if the application is trying to reference a nil optional value, reducing the time consumed by bugs of this nature.

Apart from that, Swift is open source since version 3. This means that people all over the world can now contribute to make it a better language. It will help us to have a better and safer language, driven by the needs of the community.

So, let us see some code

As we said before, Swift has a friendly syntax. Here are the basics for this language.

This single line is enough to build an app. No “import from standard libraries” are needed to make a simple console printing. Semicolons are optional. Literal strings must be written between double quotes.

var is the keyword to declare a variable, while let declares a constant. If there is no var neither let, the compiler will behave as if they existed. However, it is strongly recommended to use them for understanding purposes. If there is no var, someone reading our code may confuse a variable declaration with a simple assignment.

It is not needed to write the type of the variable, since the compiler can guess it from the value assigned. But we can make the type explicit. Also, you may declare multiple variables separated by commas.

Arrays and Dictionaries

Arrays are declared using []. They are lists of a certain type.

Dictionaries, on the other hand, are collections of key-value pairs. Like JSON objects.

As you see, when trying to access a member of the dictionary, we get an optional type. We are going to see how to deal with optional types in another section.

Conditional

When writing if statements, we need no parenthesis.

Switch

It is not recommended to use switch unless it is really necessary. However, Swift provides us with this structure.

Loops

This is a for-cycle with a fixed amount of loops.

In this example, we are creating an array of numbers using 0…9. So we can also iterate any array using a for structure.

While-loops are also provided by Swift.

Just for the record, we are converting an integer value into a string by using String(number).

Functions

Functions are declared using the func keyword. After the parameters, we put a -> operator to indicate the return type.

Functions may receive an undefined number of parameters of the same type.

Clases

Swift also supports object-oriented programming by using classes.

Properties are declared as if they were variables and methods as functions. init is the keyword used for constructors. self is a pointer that refers to the instance of the object. Just like this in most languages.

Inheritance is easy. Looks as if we were declaring a class of a certain type.

Optional Types

Swift introduces optional types. A variable of an optional type may contain a certain value, or no value at all. No value is represented by nil in Swift, just like null in other languages.

That is how we declare an optional variable. Below is how we can check whether the variable nancy has a value or not, before accessing to its property.

If we happen to be sure that an optional variable is not nil, then we can force the unwrapping of the value by using ! operator. If there is no value, a runtime exception will be thrown.

Final Words

Swift is a big step in the programming history. We are experiencing a programming language created by Apple, which is open source. Ten years ago, we could not imagine Apple having anything open source. Besides, it is a great effort in favor of the community. Swift being open source means that some day, we can run  a server-side application running on Windows, as we can do now with Node.js.

What is more, there is a framework called Vapor that helps us make  web applications. We will show how to do so by using Swift in our next post.

Share this articleShare on LinkedInTweet about this on TwitterShare on FacebookShare on Google+Email this to someone
Go Back