Chapter 2. Working with Commonly Used Data Structures
The Swift language is truly powerful, but a powerful language is nothing if it doesn't have a powerful standard library to accompany it. The Swift standard library defines a base layer of functionality that you can use for writing your applications, including fundamental data types, collection types, functions and methods, and a large number of protocols.
We're going to take a close look at the Swift standard library, specifically looking at support for collection types, with a very low level examination of arrays, dictionaries, sets, and tuples.
The topics covered in this chapter are as follows:
Using the Swift standard library
Implementing subscripting
Understanding immutability
Interoperability between Swift and Objective-C
Swift protocol-oriented programming
Using the Swift standard library
Users often treat a standard library as part of the language. In reality, philosophies on standard library design very widely, often with opposing views. For example, the C and C++ standard libraries are relatively small, containing only the functionality that every developer might reasonably require to develop an application. Conversely, languages such as Python, Java, and .NET have large standard libraries that include features, that tend to be separate in other languages, such as XML, JSON, localization, and e-mail processing.
In the Swift programming language, the Swift standard library is separate from the language itself, and is a collection of classes, structures, enumerations, functions, and protocols, which are written in the core language. The Swift standard library is currently very small, even compared to C and C++. It provides a base layer of functionality through a series of generic structures and enums, which also adopt various protocols that...
Implementing subscripting
Subscripts can be defined for classes, structures, and enumerations. They are used to provide a shortcut to elements in collections, lists, and sequence types by allowing terser syntax. They can be used to set and get elements by specifying an index instead of using separate methods to set or retrieve values.
You can define a subscript that accepts one or more input parameters, the parameters can be of different types, and their return value can be of any type. Use the subscript
keyword to define a subscript, which can be defined as read-only, or provide a getter and setter to access elements:
class MovieList {
private var tracks = ["The Godfather", "The Dark Knight", "Pulp Fiction"]
subscript(index: Int) -> String {
get {
return self.tracks[index]
}
set {
self.tracks[index] = newValue
}
...
Understanding mutability and immutability
Swift doesn't require that you define separate mutable and immutable types. When you create an array, set, or dictionary variable using the var
keyword, it will be created as a mutable object. You can modify it by adding, removing, or changing the value of items in the collection. If you create an array, set, or dictionary using the let
keyword, you are creating a constant object. A constant collection type cannot be modified, either by adding or removing items, or by changing the value of items in the collection.
Mutability of collections
When working with collection types, you need to understand how Swift treats mutability between structs and classes. Some developers tend to get confused when working with constant class instances because their properties can still be modified.
When you create an instance of a structure and assign it to a constant, you cannot modify properties of that instance, even if they are declared as variables. This is not true...
Interoperability between Swift and Objective-C
Swift has been designed to be interoperable with Objective-C. You are able to use Swift APIs in your Objective-C projects, and Objective-C APIs in your Swift projects. Apple has greatly expanded the interoperability between the two languages since Swift was originally released; there are still certain features and functionalities that are not compatible with the new languages.
Even if you're new to iOS or macOS development and have never worked with Objective-C before, and do not intend to, you should still familiarize yourself with this section. There are so many existing Objective-C frameworks and class libraries that you're bound to have to deal with interoperability at some point in time.
We will not cover all of the specific areas of interoperability between the two languages in this book, there are other great books that have been published that focus on only core Swift language features that cover them in great detail. We'll look at the...
Swift protocol-oriented programming
In Swift, you should start with a protocol and not a class. Swift protocols define a list of methods, properties, and in some cases, related types and aliases, that a type supports. The protocol forms a contract with a promise that any type that conforms to it will satisfy the requirements of the protocol. Protocols are sometimes referred to as interfaces in other languages such as Java, C#, or Go.
Protocols in Swift are a superset of Objective-C protocols. In Objective-C, all methods are resolved via dynamic dispatch at runtime using messaging. Swift, on the other hand, makes use of multiple dispatch techniques; by default, it uses a vtable, which lists available methods in the class. A vtable is created at compile time and contains function pointers that are accessed by the index. The compiler will use the vtable as a lookup table for translating method calls to the appropriate function pointer. If a Swift class inherits from an Objective-C...
In this chapter, we've learned about the difference between classes and structures and when you would use one type rather than another, as well as the characteristics of value types and reference types and how each type is allocated at runtime. We went into some of the implementation details for the array, dictionary, and set collection types that are implemented in the Swift standard library. And while not actually a collection type, we also discussed tuples and the two different types Swift supports.
We discussed how Swift interoperates with Objective-C and the C system libraries, and how Swift has added a feature called failable initialization so it can provide backward compatibility with Objective-C. Then we discussed some of the differences in how you call methods in Swift versus sending a message to an Objective-C receiver, as well as the different types of dispatching methods Swift supports. We saw how Swift supports bridging between the native Swift and Objective-C types and...