Apple has been using Objective-C as their main programming language for many years, but the company from Cupertino decided that it’s high time they underwent some “swift” changes in the way iOS and OS X developers are building apps for Apple’s operating systems. As a result, at the 2014 Worldwide Developers Conference, Apple has introduced a new programming language: Swift, which had been under development since 2010.
Introducing Swift
Swift is a multi-paradigm programming language, purposely developed by Apple for the iOS and OS X operating systems. The main thing that can be said about Swift is that it’s a remaking of the Objective-C language, using modern concepts and syntax, a remaking that promises an improved speed and security.
Described as an Objective-C without the C, Swift promises to keep the good parts and throw the bad ones, while adding what was missing in Objective-C. This resulted in more enhancements and also new features, such as:
• Automatic Reference Counting, which is a memory management mechanism somewhat similar to garbage collection (it will automatically remove unneeded information that’s sitting in the device’s memory)
• Debugging enhancements such as a read–eval–print loop. Developers can view the results of a piece of code (as they type it) in the interactive playground
• Other things that make programming a bit easier and more powerful, such as strong typing and generics
• Swift is easy to use and learn, new developers won’t have a problem learning, old developers will quickly adapt
What about Objective-C?
After announcing the Swift programming language, developers have asked themselves: what about Objective-C? (the programming language that Apple has been using since the 80s). Well, the following weaknesses might be some of the reasons why Apple has decided to make a swift escape from Objective-C:
• The pointers mechanism inherited from C makes it harder to have good guarantees about references, and this can lead to negative consequences in terms of performance and security
• It requires manual memory management, which can result in stability problems (if not done well)
• Objective-C does not have a facility for JIT (just-in time) compilation
• It has no operator overloading.
Don’t get me wrong, Objective-C is a capable programming language, but it does not seem to have the ability to keep pace with new features from other programming languages. In short, it’s getting old.
Swift is a reimagined Objective-C, so (without entering into details about the latter) let’s see what does Swift has to offer.
A closer look
Apple has decided that understanding the code is more important than writing it more easily, and so we see that Swift has adopted the trade-off which results in a more readable and understandable code (although it will be more time consuming to write it). In order to balance this, many things in Swift became optional: the method signature information that belongs to function calls, the semicolons that define the end of the line, the parentheses for logical evaluations.
In Swift, strings and numbers are treated more like objects, having properties associated with them. Also, by assigning a value to a variable, Swift will automatically initialize that variable with the type you used when declaring it.
Another improvement to Swift is getting rid of certain notorious C-like error-prone behaviors, such as: the pointers are exposed, assignments return values, variables and constants are not initialized.
Looking into the future
Many developers are arguing that it will take Swift some years to become a standard for iOS and OS X development – and so, while Swift is attractive to beginners, Objective-C veterans are not so keen to make the jump right away. In spite of this, learning Swift is profitable for beginners and veteran iOS developers alike, because when the transition between the 2 programming languages will be finished, Swift is going to remain the only programming language for iOS and OS X development.
In light of all this, Swift has its share of disadvantages though:
• It can be used only by iOS and OS X developers
• The code execution is slower than the one from Objective-C (although not in all cases)
• It needs time to become a mature programming language
In the end, developers who are worried that their Objective-C code is going to the Trash (the OS X Trash bin, of course :)) need to stop worrying, as Swift is compatible with Objective-C, meaning that the 2 programming languages can be used alongside in the same app. Apple’s intent is obvious, Swift and Objective-C will co-exist until the former will completely replace the latter.
Many developers have stated in the past that the end of Objective-C is near and that Apple needs to move forward. As a result, today we have Swift, “an elegant programming language for a more civilized age”* 🙂