Book Image

Mastering Swift 4 - Fourth Edition

By : Jon Hoffman
Book Image

Mastering Swift 4 - Fourth Edition

By: Jon Hoffman

Overview of this book

<p>Swift is the definitive language for Apple development today. It's a vital part of any iOS and macOS developer's skillset, helping them to build the most impressive and popular apps on the App Store - the sort of apps that are essential to iPhone and iPad users every day. With version 4.0, the Swift team has added new features to improve the development experience, making it easier to get the results you want and customers expect.</p> <p>Inside, you'll find the key features of Swift 4.0 and quickly learn how to use the newest updates to your development advantage. From Objective-C interoperability and ARC to closures and concurrency, this advanced Swift guide will develop your expertise and help you become fluent in this vital programming language.</p> <p>We'll give you an in-depth knowledge of some of the most sophisticated elements of Swift development, including protocol extensions, error-handling, design patterns, and concurrency. We'll guide you on how to use and apply them in your own projects. You'll see how to leverage the power of protocol-oriented programming to write flexible and easier-to-manage code.</p>
Table of Contents (24 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Free Chapter
1
Taking the First Steps with Swift
2
Learning about Variables, Constants, Strings, and Operators

Optional chaining


Optional binding allows us to unwrap one optional at a time, but what would happen if we had optional types embedded within other optional types? This would force us to have optional binding statements embedded within other optional binding statements. There is a better way to handle this, by using optional chaining. Before we look at optional chaining, let's see how this would work with optional binding. We will start off by defining three types that we will be using for our examples in this section:

class Collar { 
  var color: String 
  init(color: String) { 
    self.color = color 
  } 
} 
 
class Pet { 
  var name: String 
  var collar: Collar? 
  init(name: String) { 
    self.name = name 
  } 
} 
 
class Person { 
  var name: String 
  var pet: Pet? 
  init(name: String) { 
    self.name = name 
  } 
} 

In this example, we begin by defining a Collar class, which has one property defined. This property is named color, which is of type string. We can see that the color...