Swift features
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 table 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 once to perform identical tasks for different types of objects. |
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. |
|
The |
Tuples |
Functions can have multiple return types by using tuples. |
Operator overloading |
Classes can provide their own implementations 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. This is discussed in Chapter 10, Protocol-Oriented Design. |
Table 1.1: Swift features
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.