On June 2, 2014 at the Worldwide Developers Conference (WWDC) Apple announceed that it has developed a new programming language called Swift to succeed Objective C. Swift is available to registered developers in beta form at present and its use is supposed to be supported when Xcode’s next version is released.
What is Swift really?
According to Wikipedia,
“Swift is a multi-paradigm, compiled programming language developed by Apple for iOS and OS X development. Introduced at Apple’s developer conference WWDC 2014, Swift is designed to replace Objective-C, Apple’s object-oriented language, while working with Apple’s Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient against erroneous code. It is built with the LLVM compiler included in Xcode 6 beta, and uses the Objective-C runtime, allowing Objective-C, Objective-C++ and Swift code to run within a single program.”
Swift has resulted from several years of experience developing Apple platforms, in addition to research on languages for programming. Application programming interfaces (APIs) in Swift are simple to read and maintain as Swift has a clean syntax for named parameters brought forward from Objective C. Modules in Swift provide namespaces and eliminate headers. The code in Swift is cleaner and less prone to mistakes, mainly on account of inferred types. Swift manages memory automatically and you are not required to type semicolons.
Just like many scripting languages, the compiler in Swift gathers the variable type, which is not the case with Objective C that relies on defined pointers. However, it offers features such as operator overloading, generics and well-defined namespaces, which are quite similar to features found in Java and C++. Swift seems to depend on the dot-notation that Apple provided in one of Objective C’s earlier iterations.
Swift will depend on automatic reference counting that Apple has provided.
History of Swift
Swift began to be developed in 2010. Ideas for Swift were taken from existing languages such as Objective C, CLU, Rust, C#, Haskell, Python and Ruby.
Astronomy, biochemistry, materials science, neuroscience, mass media analysis, climate, social network analysis, earthquake simulation, economics, energy and hydrology are among the diverse domains in which Swift has been put to use fruitfully. Parallel computing is another domain where Swift has been utilized extensively for education and training. Swift has created opportunities for leveraging the contributions and skills of many undergraduate and graduate students as it has acted as a base for widening participation in scientific computing.
Why Apple chose to adopt Swift and what next?
Apple had a number of strong reasons to bring forth a completely new programming language for the iOS platform. They wanted new developers to be able to approach their platform with greater ease and to raise the stability and safety of new applications.
They realized that they had to rewrite from the ground up to overcome the burden of Objective C’s C legacy, which remained in spite of recent updates with things such as ARC.
‘Safe by default’ is the first main feature of Swift. You can expect the improvements in app stability and quality to compensate for the change in language. Through type inference and generics, the typing becomes stronger and more pervasive. Range checks on overflow operators and array access are the additional safety features provided against slack programming practices.
Operator overloading and generics are among the C++ features that C++ developers are likely to have missed in Objective C but to find in Swift and without much complexity. Several idioms found commonly in functional languages can also be found easily in Swift.
Why a new language?
Apple can build apps using third party systems based on other languages. However, Apple appears keen on developing the best tool required instead of trying to adapt something that is already available. For example, Microsoft switched to C# as the main language for .NET from C++, after they had tried Java and rejected it. They found C# far more useful than both C++ and Java. Apple is expected to benefit similarly by having their own language customized and optimized exclusively for Cocoa Touch and Cocoa development.
Bulk modification and loop configuration, among others, often lead to code repetition, whether you write traditional apps or games. Swift lets you reduce overheads for app construction by bringing into play a few functional elements.
Will Objective C go away?
This is unlikely to happen in the foreseeable future.
In Swift, you can compile code as you type it and the output is shown in a different pane of the editing window. So, you can test code fragments without having to compile an entire project again.
It will, however, still be possible to mix code in Swift with Objective C and standard C code in a project.
While retaining binary compatibility, it is likely that Swift will be subjected to some non-compatible changes in syntax. In due course of time, Swift is expected to have a number of capabilities of the Objective-C/C++/C environment.
You might still have to create some libraries in Objective C as you write code in Swift. That is because some libraries are still created in C and C++, while using Objective C. May be Swift will provide better ways of doing what you do in Objective C.
Selector based target-action pairs and key value coding are among the parts of AppKit and UIKit that aren’t similar to those available in Swift. It seems difficult, if not impossible, to write anything such as NSManagedObject in Swift. So, there are significant parts of Cocoa that depend on Objective C. Just as C is based on assembly and Objective C is based on C, Swift is based on Objective C.
Till then, there are likely to be problems that can be solved better through C or Objective C. Languages such as C have always been a good choice for solving such problems. It is not for nothing that C and C++ are still in use even after three decades. Similarly, Objective C is backed up by many years of existing developer and code expertise. It is unlikely to become redundant any time soon. Some might switch to Swift at once while others will continue to develop apps in languages that they know well and have grown to trust.
Also, Objective C and Swift are interoperable and developers could develop apps in Swift while using third party libraries to build software for iOS7 and iOS8, besides the current OS X version. You can mix and match languages, even within the same library.
So, what next?
As it happened when Microsoft switched to C#, developers using Objective C could be affected somewhat by the switch to Swift. Then, Microsoft released all new appendages for C# and the C++ developers were left high and dry. For many years, the C++ environment saw very few significant improvements.
Apple has released ‘Playground’ along with Swift and it provides a read-evaluate-print loop that covers a lot of debugging and developer feedback that those developing in Objective C won’t have access to.
Swift has considerable support and libraries and, like any other language introduced recently, can be used to develop apps.
Benefits of Swift for Businesses and Developers
1. Parallel: Swift does not require much of parallel programming that can be quite complex. It can run a number of programs simultaneously, when the inputs become available.
2. Flexible: Swift is in use in various fields of business, engineering and science. These include education, physical sciences, computer science, biological sciences, humanities and social sciences.
3. Fast: You can launch hundreds of programs per second, thousands at a time and a million programs together in Swift.
4. Easy: You can work on a large scale through simple and short scripts. The same script can be run on supercomputers, multicore computers, clouds, grids and clusters.
5. Modern: Swift has resulted from several years of experience in development of Apple platforms, in addition to the latest research on languages for programming. Application programming interfaces (APIs) in Swift are far simpler to read and maintain than those in Objective C as Swift comprises named parameters brought forward from Objective C. Swift modules provide namespaces and eliminate headers and the code in Swift is made less prone to mistakes and is cleaner on account of inferred types. Swift manages memory automatically and you are not required to type semi-colons.
You can make code in Swift more expressive on account of the following features:
- Filter, map and other such patterns of functional programming
- Function pointers unified with closures
- Structs that support protocols, extensions and methods
- Multiple return values and tuples
- Concise and fast iteration over a collection or a range
6. Always Ready: You can enhance existing features in your app or implement new ones as you can begin to use Swift code almost immediately. New Swift code is easy to adopt as it can co-exist with existing Objective C files in a single project. Also, upon the release of OS X Yosemite and iOS, you will be able to submit apps built using Swift to the Mac App Store and the App Store.
7. Interactive Playgrounds: Writing code in Swift is simplified a lot using Playgrounds. You can see the results immediately after typing in a line of code. You can watch the progress of your code through the timeline assistant if your code runs over time, for example, through a loop. The timeline can play animated SpriteKit scenes and shows variables in a graph, besides drawing each step while composing a view. You can simply move code into a project after having perfected it in the playground. Playground lets you:
- Design new algorithms while looking at the results at each step
- Create tests and verify those before moving to your test suite
- Experiment with APIs to improve your skills in Swift coding
8. Powerful: Swift has been built to be quick from the earliest point of its inception. Swift code changes into optimized native code through the use of the LLVM compiler optimized for high performance. It can get the best out of iPad, iPhone and Mac hardware. The standard library and syntax have been tuned in such a manner that you can not only write code in the way that is most obvious but also write code that performs the best.
Swift combines the best features of the Objective-C and C languages. It includes low-level primitives like operators, flow control and types. It gives Cocoa Touch and Cocoa developers the power and performance they need through the provision of object-oriented features such as generics, protocols and classes.
9. Safe: Swift initializes variables before use, checks integers and arrays for overflow and manages memory automatically. Swift tunes syntax to simplify defining your intent. For instance, a variable or constant is defined using three-character keywords.
Swift has safe patterns that are tuned for powerful APIs in Cocoa Touch and Cocoa. Swift code makes it very easy to comprehend and properly handle cases in which objects are nil. Entire lines of code in Objective C can be replaced by adding a single character in Swift. All of that makes the development of Mac and iOS apps much safer and simpler than before.
10. Read-Evaluate-Print Loop: An interactive version of Swift is built into the debugging console in Xcode. You can write code afresh to look at how it functions in a script-like environment or use syntax in Swift to evaluate your running app. This is available in Terminal and also in the Xcode console.
Swift is a new programming language for Cocoa Touch and Cocoa and provides an expressive yet concise syntax to make writing interactive and simpler. Swift code works alongside Objective C code and can be added to your current apps, besides being ready for fresh OS X and iOX projects. The apps written in Swift run considerably faster than those written purely in Objective C.
Have you used Swift yet and, if so, how has your experience been? Please share with us in the comments section below.