When Apple first introduced Swift, it said that Swift is Objective-C without the C. This really only tells us half of the story. Objective-C is a superset of C and provides object-oriented capabilities and a dynamic runtime to the C language. This meant that with Objective-C, Apple needed to maintain compatibility with C, which limited the enhancements it could make to the Objective-C language. As an example, Apple could not change how the switch statement functioned and has still maintained compatibility with the C language.
Since Swift does not need to maintain the same C compatibility as Objective-C, Apple was free to add any feature/enhancement to the language. This allowed Apple to include the best features from many of today's most popular and modern languages, such as Objective- C, Python, Java, Ruby, C#, and Haskell.
The following chart shows a list of some of the most exciting enhancements that Swift offers compared to the Objective-C language:
Swift feature |
Description |
Type inference |
Swift can automatically deduce the type of a variable or constant, based on the initial value. |
Generics |
Generics allow us to write code only once to perform identical tasks for different types of object. |
Collection mutability |
Swift does not have separate objects for mutable or non- mutable containers. Instead, you define mutability by defining the container as a constant or variable. |
Closure syntax |
Closures are self-contained blocks of functionality that can be passed around and used in our code. |
Optionals |
Optionals define a variable that might not have a value. |
Switch statement |
The Switch statement has been drastically improved. This is one of my favorite improvements. |
Tuples |
Functions can have multiple return types using tuples. |
Operator overloading |
Classes can provide their own implementation of existing operators. |
Enumerations with associated values |
In Swift, we can do a lot more than just define a group of related values with enumerations. |
Protocols and protocol-oriented Design |
Apple introduced the protocol-oriented programming paradigm with Swift version 2. This is a new way of not only writing applications but also changing how we think about programming. |
Before we begin our journey into the wonderful world of Swift development, let's take a detour and visit a place that I have loved ever since I was a kid: the playground.