In this chapter, we are focusing on collection and closure changes in Swift 3. Collections are important to all programming languages because they allow you hold groups of related items. Closures are also important to Swift because they give you the ability to pass around functionality to be used in a different location of your code. There are several nice additions that will make working with collections even more fun. We will also explore some of the confusing side effects of creating closures in Swift 2.2 and how those have been fixed in Swift 3.
You're reading from Swift 3 New Features
Let's begin our discussion with Swift 3 changes to Collection and Sequence types. Some of the changes are subtle and others are bound to require a decent amount of refactoring to your custom implementations. Swift provides three main collection types for warehousing your values: arrays, dictionaries, and sets. Arrays allow you to store values in an ordered list. Dictionaries provide unordered key-value storage for your data. Finally, sets provide an unordered list of unique values (that is, no duplicates allowed).
Arrays, dictionaries, and sets are implemented as generic types in Swift. They each implement the new Collection protocol, which implements the Sequence protocol. Along this path from top-level type to Sequence protocol, you will find various other protocols that are also implemented in this inheritance chain. For our discussion on flatMap
and lazy flatMap
changes, I want to focus on Sequences.
Sequences...
Swift 3 introduces a new model for collections that moves the responsibility of the index traversal from the index to the collection itself. To make this a reality for collections, the Swift team introduced four areas of change:
The Index property of a collection can be any type that implements the Comparable protocol
Swift removes any distinction between intervals and ranges; leaving just ranges
Private index traversal methods are now public
Changes to ranges make closed ranges work without the potential for errors
Note
You can read the proposal at the following link https://github.com/apple/swift-evolution/blob/master/proposals/0065-collections-move-indices.md
I covered a lot of stuff in a just a few pages on collection types in Swift 3. Here are the highlights to keep in mind about the collections and indices.
Collection types (built-in and custom) implement the Collection protocol.
Iterating over collections has moved to the Collection - the index no longer has that ability.
You can create your own collections by adopting the Collection protocol. You need to implement:
startIndex
andendIndex
properties,The subscript method to support access to your elements
And the
index(after: )
method to facilitate traversing your collection's indices.
A closure in Swift is a block of code that can be used in a function call as a parameter or assigned to a variable to execute their functionality at a later time. Closures are a core feature to Swift and are familiar to developers that are new to Swift as they may remind them of lambda functions in other programming languages. For Swift 3, there were two notable changes that I will highlight in this section. The first change deals with inout captures. The second is a change that makes non-escaping closures the default.
In Swift 2, capturing inout
parameters in an escaping closure is difficult for developers to understand. Some closures are assigned to variables and then passed to functions as arguments. If the function that contains the closure parameter returns from its call and the passed in closure is used later, then you have an escaping closure. On the other hand, if the closure is only used within the...
In this chapter we covered changes to collections and closures. We learned about the new Collection protocol that forms the base of the new collection model and how to adopt the protocol in our own custom collections. The new collection model made a significant change in moving collection traversal from the index to the collection itself. The new collection model changes are necessary in order to support Objective-C interactivity and to provide a mechanism to iterate over the collections items using the collections itself. As for closures, we also explored the motivation for the language moving to non-escaping closures as the default. We also learned how to properly use inout
parameters with closures in Swift 3. In the next chapter, we are will cover more type changes and type aliases within protocols and protocol extensions.