As you may have heard, Apple unveiled a new programming language called Swift a few weeks back at WWDC. This is monumental in a number of ways, and one of the big takeaways from the announcement, though not verbalized as such, is that Objective-C is dead. Contrary to what you may think, this is actually a good thing, and we will take a closer look at Swift to find out why.
With Swift, you can actually express less of the programs that may be expressible in Objective-C. This may sound like a detriment at first, but it is one of the big reasons that Swift is superior. Looking at it another way, Swift makes it impossible to express a number of incorrect programs that would have been expressible in Objective-C. This is due to its much stronger type system, which we will explore in a minute. Swift also protects from a number of other bugs that could pose security risks, such as buffer overflows and unbraced if statements that led to the now infamous goto fail bug. This added safety will result in less bugs, reduced testing time, more stable applications and ultimately better experiences for end users.
Saying that Swift is more expressive than Objective-C may seem strange, given that we just said that Swift can express less programs than Objective-C, but it’s true. With Swift, it is possible to express many things that were either impossible in Objective-C, such as the new Swift Enums and Structs, or highly dangerous, due to the weak type system. Whole new worlds of abstractions and design patterns that were not heard of in Objective-C are now possible in Swift, and this will open the doors for a much better toolchain, which will, again, enable the creation of much better apps and experiences for end users.
Strong Static Typing
We just talked about how Swift is more expressive than Objective-C, and we noted that this is partly due to its strong static type system, but how is a static type system better than a dynamic one like Objective-C’s? First, we need to clear up the misconceptions involving ‘static’ and ‘dynamic’ type systems. The names ‘static’ and ‘dynamic’ may be unfortunate misnomers, as static evokes images of the boring and lifeless, while ‘dynamic’ invokes images of the fun and exciting. However, when talking about languages and type systems, there is actually only one type of system, and that is a static type system. The distinction between static and dynamic is that dynamic type systems are actually static type systems that have only one type, whereas a (good) static type system has many expressible types. So what benefits do static type systems give us? As seen above, safety is a big win, but with Swift’s type system, it also becomes possible to express many more useful abstractions, safely, than were possible in Objective-C.
So with all these improvements, you may be wondering if you still need to hone your Objective-C skills, or if new developers should learn Objective-C alongside Swift. I would say the answer to both of these questions is a definite and emphatic no. Apple did not create Swift to give developers an extra choice, but they created the programming language in order to replace Objective-C. While you may not see it this year or next, you can be sure that there will soon be Swift only API’s from Apple. Furthermore, taking advantage of the latest and greatest features of either iOS or OS X will only be possible while using Swift. So the sooner you get on the Swift train, the better, both for you as a developer and for your users.