The Swift Programming Language
Swift is the new language from Apple and surely the youngest programming language in the TIOBE index. Let's see what benefits it really brings to developers and why, to start with, Apple needed it
The Swift programming language is a computer programing language first appeared on the scene 10 years ago (on June 2, 2014). It is a compiled, high-level, general-purpose, statically, strongly typed, object-oriented, protocol-oriented, imperative, functional and procedural programming language with almost automatic memory management whose main purpose is to be a programming language for Apple iOS, macOS, watchOS, tvOS, and Linux supporting many core concepts associated with Objective-C like dynamic dispatch, widespread late binding, extensible programming created to be a more modern alternative to Objective-C. Swift uses Objective-C runtime library and allows C, Objective-C, C++ and Swift code to coexist and run within one program. Swift was invented by Chris Lattner at Apple.
First glance at the code
Example: The classic "Hello world" program written in Swift:
Main features of Swift
Statically, strongly typed
Swift has a type system that is checked at compile time, which means that every variable has an explicit pre-assigned type and comes at run-time with the guarantee that the type of the values that can be assigned to that variable cannot be incompatible with the original type it has been pre-assigned. At the same time Swift uses type inference extensively, allowing to omit the type or part of the type of many variables and expressions in the code.
In Swift there are two kinds of types: named types and compound types. Named types include classes, structures, enumerations, and protocols and the Swift standard library defines many commonly used named types, including those that represent arrays, dictionaries, and optional values. Compound types are types without name, defined in the Swift language itself. There are two compound types: function types and tuple types. A compound type may contain named types and other compound types.
Data types that are normally considered basic or primitive in other languages—such as types that represent numbers, characters, and strings, are actually named types, defined and implemented in the Swift standard library using structures. These are:
|a single Unicode character
|a collection of type Character
|2 bits wide on 32-bit systems and 64 bits wide on 64-bit systems
|32 bits floating point number
Swift is object-oriented. It supports common object-oriented patterns, has classes, supports generics and protocols are like interfaces in other languages. Contrary to other languages there is not a base class from which all other classes are automatically derived.
Swift supports multi-threading with the Grand Central Dispatch (GCD) and dedicated types, member functions and properties for concurrent and parallel programming.
Swift uses Automatic Reference Counting (ARC) to manage memory. While this is way better than Objective-C where memory management was totally manual, there is still need in Swift to manually use special keywords like weak and unowned to prevent strong reference cycles.
Swift has its on Standard Library, but another project, The Swift Core Libraries project, provides for all the platforms that Swift supports higher-level functionality than the Swift Standard Library.
Swift is a compiled language like Objective-C, but it also has a static type system, which means that it should be easier for the compiler to optimize the execution speed of Swift programs. In fact initial Apple press releases when presenting the language talked of speeds that were 3.6 time better than those of comparable Objective-C programs. This has never been confirmed by independent results, which instead seem to suggest that Swift and Objective-C execution speeds are almost the same.
Being a compiled language, Swift doesn't need any execution environment, other than the operating system, to run.
Free and open source
Swift was born as a closed and proprietary language but since version 2.2 Apple released it under the free and open source Apache 2.0 license.
Best books for learning Swift
|Swift Programming: The Big Nerd Ranch Guide (2nd Edition) (Big Nerd Ranch Guides)
Carefully constructed explanations and examplesAfter working through the book, you will have the knowledge and confidence to develop your own solutions to a wide range of programming challenges using Swift... (continue on Amazon)
For experienced programmers
|Advanced Swift: Updated for Swift 4
From low-level programming to high-level abstractionsSwift is a great language for systems programming, but also lends itself for very high-level programming. We’ll explore both high-level topics (for example, programming with generics and protocols), as well as... (continue on Amazon)
Swift is a very young language to make any conclusion at all about it. Surely Apple felt that Objective-C was not enough any more or, to be more precise, was not enough modern any more if compared to Java, C# and the like. The point is that despite the syntactic sugar and the overall design of the language, which surely results in a more modern language that Objective-C, there are things that still faze for a language that wants to be defined as truly modern, like memory management which is certainly still too cumbersome.