Reader small image

You're reading from  Swift 3 New Features

Product typeBook
Published inOct 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786469632
Edition1st Edition
Languages
Right arrow
Author (1)
Keith Elliott
Keith Elliott
author image
Keith Elliott

Keith Elliott is a multitalented professional with unique business and technology experience spanning telecommunications, real estate investment banking, and capital markets. His work is driven simply by problems that need solutions, whether the problem is as simple as his wifes request for a custom to-do list or as complex as interest rate derivatives and foreign exchange hedging. He graduated with an MBA from Columbia Business School with an emphasis in entrepreneurship and an undergraduate degree from Georgia Institute of Technology with a bachelor's in computer engineering. Keith's own company, GittieLabs LLC, works with startups to provide technology solutions. His vision is to equip students with the real-life experience necessary to succeed in startup and corporate life. You can find his blog on the GittieLabs LLC website, www.gittie.com. On nights and weekends, Keith can be found spending time with his family, riding motorcycles with his lovely wife, watching football, and rewatching countless hours of WWDC videos.
Read more about Keith Elliott

Right arrow

Chapter 6. Extra, Extra Collection and Closure Changes That Rock!

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.

Collection and sequence type changes


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).

Lazy FlatMap for sequence of optional [SE-0008]

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...

A new model for collections and indices [SE-0065]


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

Introducing the Collection protocol

In Swift 3, Foundation collection types such as Arrays, Dictionaries, and Sets are generic types that implement the newly created Collection protocol. This change was needed in order to support traversal on the collection. If you want to create custom...

Quick takeaways


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 and endIndex properties,

    • The subscript method to support access to your elements

    • And the index(after: ) method to facilitate traversing your collection's indices.

Closure changes for Swift 3


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.

Limiting inout Capture of @noescape Closures [SE-0035]

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...

Summary


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.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Swift 3 New Features
Published in: Oct 2016Publisher: PacktISBN-13: 9781786469632
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Keith Elliott

Keith Elliott is a multitalented professional with unique business and technology experience spanning telecommunications, real estate investment banking, and capital markets. His work is driven simply by problems that need solutions, whether the problem is as simple as his wifes request for a custom to-do list or as complex as interest rate derivatives and foreign exchange hedging. He graduated with an MBA from Columbia Business School with an emphasis in entrepreneurship and an undergraduate degree from Georgia Institute of Technology with a bachelor's in computer engineering. Keith's own company, GittieLabs LLC, works with startups to provide technology solutions. His vision is to equip students with the real-life experience necessary to succeed in startup and corporate life. You can find his blog on the GittieLabs LLC website, www.gittie.com. On nights and weekends, Keith can be found spending time with his family, riding motorcycles with his lovely wife, watching football, and rewatching countless hours of WWDC videos.
Read more about Keith Elliott