In WWDC 2014 when Apple introduced Swift, a multi-paradigm language created by Apple, many developers thought Swift would substitute Objective-C for iOS and Mac development. Swift has seen several changes since then and evolved into what we see today. However, many iOS developers are still skeptical about the power of this language and continue to develop applications in Objective-C. The truth is, Swift is an evolution of Objective-C and was intended to be the future of the platforms (and not just for mobile). The Swift community is extending use in server-side and embedded systems and even Google is considering adopting Swift as the primary language for Android development! In this post, I will explain why you should choose Swift and how to start.
Why choose Swift?
Although Objective-C has been the de facto language for iOS development for around 10 years, a quick Google search will give you hundreds of reasons why you should choose Swift over Objective-C from both technical and business perspectives.
To start, Swift is a safer language
This is because it has the ability to prevent a whole category of mistakes by telling us iteratively what we’re doing wrong. Let’s look at a quick example of this. Objective-C developers will remember when trying to call a method with a pointer variable that is nil (or initializable), nothing happens. This is because the expression becomes a no-operation statement. In some ways, this is beneficial because the program won’t crash. However, it’s actually a huge source of bugs. Instead, the optionals usage in Swift makes the possibility of a nil optional value very clear. This means it can generate a compiler error if you write bad code. This creates a short feedback loop and allows programmers to code with intention.
As a modern language, Swift is easier to read and write
Swift lacks the verbosity of Objective-C in methods, and this makes it much easier to maintain. For example, the two-files approach is dropped in Swift because it combines the Objective-C header file (.h) and implementation file (.m) into a single code file (.swift). Click here to see how even you can write a one-line singleton in Swift! Another cool feature that Swift includes in its set of components is string interpolation. Now you don’t need to memorize tokens when concatenating strings. Swift also allows programmers to insert variables directly to a user-facing string, such as a label or button title. These are just a few examples of how Swift lowers the barrier to entry for many people, making it easier to focus on concepts than on unusual syntax.
Type inference allows the programmer to overlook annotating variables with type information because the compiler infers it from the value set to the variable. Also, forget about mutability, now there are no “mutable” counterparts to Array and Dictionary. Using the standard
let (collections cannot change size) and
var (opposite case) will cover those cases.
One of the biggest changes that hasn’t received enough attention is the switch from static (which are updated at major point releases) to dynamic libraries. Dynamic libraries are executable chunks of code that can be linked to an app. This feature allows current Swift apps to link against newer versions of the Swift language as it evolves over time.
Almost everything in the iOS SDK is built in Objective-C and works best with the Objective-C programming model in mind. However, most of the components are changing during the maturity process of Swift. A couple of years ago Swift was a child that could barely crawl, now it’s a very robust teenager standing on its feet. Most of the third-party libraries you can find out there are written in Objective-C. However, it hasn’t taken too much time for developers to rewrite those libraries because the most future-proof projects are those written in Swift.
In the past two years, Swift usage has expanded to be utilized for a third of all open source Cocoa projects. At this rate, in a couple of years, it will have the same coverage as Objective-C, perhaps even more! This includes different resources, from books, articles, online courses and specializations. Nevertheless, there are a lot of cool features packaged in Swift, that even including in this article would be overwhelming to outline all at once.
Where and how to start?
Here, I’d like to point out that it’s good to know both languages. This gives developers much more flexibility in any task or environment they may be assigned. Learning how to use Swift for Objective-C developers will not be as hard as some people think if you already code using the latest Objective-C coding style (using
array[n] to access index instead of
[array objectAtIndex: n] statements). Once you are a rock-solid Objective-C developer, you will not notice the change after two weeks of usage. If you want to start playing with the features of Swift, you can start with Xcode Playgrounds, which is an environment that’s part of Xcode. It allows you to learn and explore Swift, prototype parts of your app, and create learning environments for others. There is even an iPad app available to download and do some lab-like experiments.
Another approach to start is going through the Apple book and documentation available online. This will guide you through the first steps using the syntax, differences, and even cool features the language has. Finally, don’t wait too long to start experiencing the power of the cutting-edge Swift language!
If you are interested in learning more about Objective-C and Swift take a look at Moises’ slides from his presentation on Feb 28th at the mobile #GorillaMeetups here. If you have any questions don’t hesitate to leave a comment below.