1. Introduction to Swift
Swift is a programming language developed and distributed by Apple Inc. It was released to general public in September 2014. Swift is open-source and licensed under Apache 2.0. Swift is a programming language for macOS, iOS, watchOS and tvOS. Swift is a multipurpose and multi-paradigm programming language used for developing applications for the devices running the different operating systems stated above. Swift is free to use, and Apple provides a large collection of guides, documentation and applications to start developing with Swift.
Swift has a lightweight syntax, that paired with pattern matching and typing inference gives Swift an advantage over other older and modern programming languages. This not only makes Swift code easier to write but also easier to maintain. Though Swift was developed for devices running macOS, iOS, watchOS and tvOS, it is now being used to develop operating systems, applications and other software for desktops and servers. Swift is a multi-paradigm language and does not adhere to any one specific paradigm. It supports Object-Oriented, Functional, Protocol-Oriented and other programming styles. This is one of the reasons that made Swift popular amongst the programmers. The latest stable release of Swift is version 4.2 that was released to general public on 17th September 2018 and is available as a part of Xcode 10 or on:
https://swift.org/download/
Swift has many different features that are interesting and distinguish it from other programming languages. Some of them are memory safety by default, multiple return values, Generics, use of Unicode characters as variable, class or function names and many others. Some of these will be discussed in this paper as we move forward. [1][2]
2. History of Swift
Chris Lattner started working on Swift as a side project while he was pursuing PhD as a research assistant at University of Illinois. Chris wanted to develop a language that was free of C and C++ heritage and did not need a pre-processor. Swift was initially called ‘LLVM’ and Chris was hired by Apple in 2005 to work on it. The main motive for a new programming language or Swift was the secure management of memory. Apple development community used Objective-C as their main development language and improved it in considerable ways. The drawback of Objective-C that made Swift possible was the insecure code or management of memory. Objective-C is built using C programming language and thus Swift being free of the C and C++ heritage gave Apple and Chris another reason to develop LLVM or what later came to be known as ‘Swift’.
Swift was announced at Apple’s Worldwide Developer Conference (WWDC 14) on June 2, 2014 as a developer beta before it’s general public release (version 1.0) in September 2014. Initially, Swift wasn’t open-source. After another major release (version 2.0) at WWDC 15, Swift was open-sourced in December 2015 and still remains open-sourced. Swift 3.0 was shown at WWDC 16 and brought some welcomed changes like a migration tool to migrate older code written in Swift 1.x and 2.x to Swift 3.0 and later. It also introduced a Swift standard library which defines a base layer for writing programs. [1][3][4]
3. Paradigms
Swift is a programming language that supports many programming paradigms, so it is referred to as a multi-paradigm programming language. The paradigms supported by Swift are Object-Oriented, Functional, Imperative and Protocol-Oriented to name a few. While Swift handles all of these programming styles very well, some of them are better handled than others. Programmers can also mix-and-match different paradigms in their programs to achieve the goal they are pushing towards.
Though, Swift was designed to move away from C, C++ and Objective-C, it supports all of the major concepts of object-oriented programming. The Swift standard library supports object-oriented paradigm very well. From keeping the state information of the objects private (encapsulation), or ability of objects to take up many different states (polymorphism) to derivation of properties from another class (inheritance), Swift is a language that handles object-oriented concepts really well. Swift also supports overriding (overriding default behavior of a class to provide a tailored behavior from a method). To accomplish object-oriented design in a program, Swift doesn’t require anything extra to enable a programmer to style his program around object-oriented paradigm. [5]
Another programming paradigm supported by Swift is functional programming. Functional programming is a programming style in which we try to style everything like mathematical functions. Swift is not a functional language, but it allows the developer or programmer to use functional principles. Two main principles of functional programming are no mutable data and no hidden and implicit states. While, not being a functional language, Swift still handles concepts or ideas of functional programming style pretty well. [6]
The next paradigm where Swift really shines is Protocol-Oriented programming style. Protocol-Oriented programming came in to the limelight when Apple talked about it during WWDC ’15.
“A protocol defines a blueprint of methods, properties… The protocol can then be adopted by a class, structure, or enumeration” — Apple [13]
So, protocol is like a driving instructor, he can teach you how to drive a car, but he can’t control the wheel. Swift checks for the errors in protocols or in the code at compile time which allows the programmers to find the bugs before the program is even launched. Protocols defines some methods and their properties and any function that adopts that protocol has to fill in those methods and properties with appropriate values. [7]
While these are the three most used programming paradigms used by the developers while developing using Swift, there are other styles like imperative, block structured, etc that are supported by Swift. Thus, Swift doesn’t limit the developers into one programming style but rather opens up a whole different set of possibilities while staying true to its roots (idea of moving away from C, C++ and Objective-C legacy).
4. Interesting and distinguished features of Swift
1. Objective-C Compatibility and Interoperability
Swift is interoperable with existing features of objective-c and is compatible with the code written in Objective-C. It does this by providing complete access to the Objective-C APIs. Swift supports late binding, dynamic dispatch and other notable features of Objective-C. Swift also supports Objective-C and Swift code in a single program. This can be used to write an application, or a program written with a mix of both languages. It is also possible to import an Objective-C program into Swift and replace the bits of a program or use features of Swift just by replacing the parts of the Objective-C code. Apple really tried to keep the process of migrating the Objective-C code to Swift simple and they kind of succeeded. To import an existing codebase written in Objective-C, the developer just has to launch XCode (IDE) and choose his or her existing Objective-C project and add a Swift file to it. XCode will ask the developer to confirm if he or she wants to create a “Bridging Header” between the two languages. The developer can also create a bridge header manually. [8]
2. Open-source
Initially, Swift wasn’t open-source. Swift was open-sourced in December 2015 with the release of Swift version 2.2. [9] This is really helpful as this allows the consumers using the language to check out the lower-level implementation details of swift language or how a specific library or a function works. The complete Swift commit history and code is available on GitHub:
https://github.com/apple/swift
Swift is licensed under Apache 2.0 license, has over 635 contributors and 79,861 commits. [9] Being open-source also gives Swift another advantage, the developers or the community can file bugs or problems with the language and other users can recommend or post fixes for those problems. This really speeds up the process of improving the language without the users having to wait on Apple to release an update with bug fixes and other improvements. For example, here’s a bug that was filed by a user and fixed by another user of Swift.
https://bugs.swift.org/projects/SR/issues/SR-9065
This really shows the true power of Swift being open-source and the active development community.
3. Memory safe
Swift is memory safe. This is important in today’s world as the number of attacks or simply put software breaking bugs can leave a bad experience. By default, Swift blocks access to the areas of memory that have already been deallocated, doesn’t allow variables to be used before they are initialized and other such features without the programmer needing to worry about them. Swift also provides exclusive access to memory so that there are no conflicts if two functions have to access (write) to a same variable in a multithreading environment. A programmer can turn off certain memory safety features of Swift, but it is highly advised not to do so and is to be done at the expense of the programmer. Swift uses a dataflow analysis technique to ensure a programmer doesn’t use a variable before initializing it.
This is called “Definite assignment analysis”. [14] [10]
In the code example below, Swift doesn’t let a variable to be used without first initializing a value for that variable. It throws an error saying “Constant ‘carSpeed’ used before being initialized”.
Source: Self
4. Unicode characters in variable, constant, class and function names
Using Unicode characters as variable, class or function names might not seem an important feature but it’s definitely an interesting one. Swift allows developers to define function, class, variable names with Unicode characters also known as ‘Emojis’. For some developers it might also be an important feature that improves readability of code in various scenarios. Unicode characters can also be used in strings in Swift. To include a Unicode symbol in a program, the programmer just needs to press Command + Control + Spacebar on the keyboard and XCode shows the standard list of emojis that the programmer can choose from. The programmer doesn’t have to include any special libraries or turn on special options to enable this. Unicode characters are standard everywhere, so a programmer also doesn’t have to worry about if the code will work on other computers. The Unicode characters can improve readability. For example, if a programmer defines the name of a variable using a symbol of the car (🚗), then some other programmer can quickly see the car shown anywhere in the program and can infer that the variable is being called or used in that part of the program. [11]
Source: Self
5. Generics
Generics allows the programmer to define functions without specifying the type of the variable arguments so that the function can be used with different type of variables. This in turn makes the code reusable and more abstract. Generics might be compared to the C++ templates but are much easier to use and define. Generics are an important part of the swift language. Arrays, dictionaries and other data structures can be generics and need not need a type when being defined. Swift has type inference, so the types are automatically determined on the initialization of the variables. This makes the code written in Swift safe and predictable. Though generics is not a new concept, Swift’s type system makes it even more powerful.
“Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code.” – Apple [12]
The swift standard library contains the data types and the data structures amongst other important functionalities that the language supports. Thus, even the core of Swift is built on generics.
6. Syntax promoting readability
Readability is one of the most important features of any programming language and it all starts with the basic syntax of the language. Swift has a really simple syntax which is easy to understand for a person who is just starting with Swift language or even programming in general. For example, in Swift a programmer defines the constants using the ‘let’ keyword and variables using the ‘var’ keyword. Using ‘let’ to define a constant feels natural while typing the code. Swift doesn’t require you to declare the type of the constant or the variable, the type is automatically assigned when the variable is assigned a value. This leads to code that is concise because the developer doesn’t have to specify a type for each declaration. Another thing that the developers can do to increase the readability of their code is to use Unicode characters in their code as the names of variables, functions and classes. Even though the syntax of Swift is easy, it provides some really powerful features (memory safety) to make the code safe without compromising the readability.
Source: Self
7. Protocol-Oriented programming
Protocol-Oriented programming is starting to gain momentum in the programming community and is a really important part of the Swift programming language. While objected-oriented programming is going nowhere, Apple is betting on protocol-oriented programming to be the programming style for the next decade. Protocol-Oriented programming was introduced to the developers when Apple demonstrated it during WWDC’ 15 using Swift. [7] The main problems that protocol-oriented programming tries to tackle are mostly related to object-oriented programming. These are namely multiple inheritance, tight coupling between classes and problems related to testing due to these issues. [15]
For example, if a developer defines a UI controller and requires only certain specific classes that inherit from that controller to have specific functionality of the controller, it will be either very difficult or impossible to do this using object-oriented programming without having to include that functionality in all the classes that inherit from UI controller. This is where protocol-oriented programming comes in. With the protocol extensions, the developer could define a protocol and every class that conforms to that protocol will automatically gain the functionality defined in the protocol extension.
5. Evaluation of Swift
I started learning Swift in the summer of 2018 for iOS application development and was surprised with how clean and easy the syntax of Swift was. I have programmed in C, C++, Java, Python, Javascript, PHP, HTML and JSON, and can say that Swift was the language that I picked up faster than any of the languages mentioned. Writing code in Swift is interactive and fun but this does not make Swift weak as compared to other languages. Defining variables, selection statements, arrays, functions, etc. is really simple in Swift due to its Syntax and type inference. The syntax is concise and running the code feels lightning fast. Another advantage of Swift is that it is cross-platform. An application written in Swift can run on macOS, iOS, WatchOS and tvOS with little to no modifications to the code. Swift is easy to install on a machine running macOS or Linux. Anyone can install XCode from the Mac App Store and can start programming in Swift in under 10 minutes. XCode is the IDE used to program in Swift and has been around for over a decade now. Thus, XCode feels polished than other IDEs out there. I learned to program in Swift completely by myself by referring to the documentation and tutorials available online. Documentation is one of the major selling points of a programming language and Apple is doing that just right. The amount of official and unofficial documentation that is available for Swift is kind of exhilarating. From Swift Playgrounds (application to learn programming and Swift for beginners) on iPad to official lower-level documentation available on Swift website, Apple is really betting on Swift to be the next big programming language. The swift documentation can be found at:
https://swift.org/documentation/#the-swift-programming-language
I’ve been developing with Swift for only a couple of months now but the amount of developers that have been adopting Swift as their language of choice to program or even learn to program is exciting to see. The documentation and the applications like Swift Playgrounds might be one of the reasons behind Swifts rapid growth in the development world. Apple created Swift to slowly move away from Objective-C, its complicated syntax and Swift does that perfectly. While doing that, Swift almost fixes all the problems (complicated syntax, memory safety, single platform, etc.) that Objective-C had.
One of the disadvantages of Swift as compared to Python is while Swift is powerful it only has a small number of libraries. Python has a large number of libraries that fulfil needs ranging from scientific to business. One of the major features of Swift is the interoperability with Objective-C and this allows developers to use Objective-C libraries in Swift, but this can sometimes be buggy and can break a few things. Another major drawback of Swift is that it requires a machine running macOS or Linux to install and code on. This leaves out a huge chunk of developers that use Windows as their platform of choice to program or develop. On the other hand, Kotlin, that is another newer programming language is available on Windows, Linux and Mac. [16] The next drawback of Swift is that it is not backwards compatible. Code written in Swift 4.x is not compatible with Swift 2.x and 3.x. Swift wasn’t forward compatible either but that issue was fixed by Apple in the big 4.x update.
Though Swift is gaining popularity amongst the developers, it can only be used to develop applications for iOS, macOS, watchOS, tvOS and some other server applications on Linux. If Apple, made Swift applications compatible to work on more devices like Windows devices, Android, etc. this would result in a major increase in the adoption rate of Swift. After all, not everyone wants to learn a language that can cater to only a certain percent of the population.
I’ve been developing for Android for a few years now and have always been a fan of open-source software and technology. Seeing, how Apple open-sourced Swift was a shocking moment not only for me but for the whole development community and Apple followers. It was shocking because Apple is known for closing down its stuff to the last bit. Like mentioned earlier in this paper, once Swift went open-source it attracted a lot of attention from the community. Objective-C will definitely be phased out by Apple in a few years, after they realize that not much can be improved with it. Swift will be the next language that Apple will choose to go ahead with or at least I am betting on it to be.
References:
[1] Wikipedia, https://en.wikipedia.org/wiki/Swift_(programming_language), accessed September 22, 2018
[2] Swift website, https://developer.apple.com/swift/, accessed September 22, 2018.
[3] Pluralsight, https://www.pluralsight.com/blog/software-development/swift-history, accessed October 7, 2018.
[4] Wikipedia (Chris Lattner), https://en.wikipedia.org/wiki/Chris_Lattner, accessed October 7, 2018.
[5] Skookum website, https://skookum.com/blog/swift-blurs-the-lines-of-programming-paradigms, accessed October 13, 2018.
[6] Medium article, https://medium.com/@geisonfgfg/functional-swift-41f1bed646d, accessed October 13, 2018.
[7] CleanSwift website, https://clean-swift.com/protocol-oriented-principle/, accessed October 13, 2018.
[8] Medium article, https://medium.com/ios-os-x-development/10-things-i-like-about-swift-7bbd40cabb79, accessed September 23, 2018.
[9] Apple GitHub, https://github.com/apple/swift, accessed September 23, 2018.
[10] Swift memory safety page, https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html, accessed September 23, 2018.
[11] Today I Learned, https://til.hashrocket.com/posts/e1f8996fa4-emoji-variables-in-swift- accessed September 23, 2018.
[12] Swift generics page, https://docs.swift.org/swift-book/LanguageGuide/Generics.html, accessed September 23, 2018.
[13] Swift protocols page, https://docs.swift.org/swift-book/LanguageGuide/Protocols.html, accessed October 13, 2018.
[14] Definite assignment Wikipedia page, https://en.wikipedia.org/wiki/Definite_assignment_analysis , accessed November 4, 2018.
[15] WWDC ’15, Apple Official YouTube channel, https://www.youtube.com/watch?v=_p8AsQhaVKI, accessed November 10, 2018.
[16] Kotlin official website, https://kotlinlang.org, accessed November 10, 2018.