Mastering Swift 5.3
eBook - ePub

Mastering Swift 5.3

Upgrade your knowledge and become an expert in the latest version of the Swift programming language, 6th Edition

  1. 418 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Swift 5.3

Upgrade your knowledge and become an expert in the latest version of the Swift programming language, 6th Edition

Book details
Book preview
Table of contents
Citations

About This Book

A comprehensive guide for programming enthusiasts who wish to gain a firm command of the fundamentals and advanced Swift concepts

Key Features

  • Sixth edition of this bestselling book, improved and updated to cover the latest version of the Swift 5.3 programming language
  • Get to grips with popular and modern design techniques to write easy-to-manage Swift code
  • Use core Swift features such as concurrency, generics, and copy-on-write in your code

Book Description

Over the years, Mastering Swift has proven itself among developers as a popular choice for an in-depth and practical guide to the Swift programming language. This sixth edition comes with the latest features, an overall revision to align with Swift 5.3, and two new chapters on building swift from source and advanced operators.

From the basics of the language to popular features such as concurrency, generics, and memory management, this in-depth guide will help you develop your expertise and mastery of the language.

As you progress, you will gain practical insights into some of the most sophisticated elements in Swift development, including protocol extensions, error handling, and closures. The book will also show you how to use and apply them in your own projects. In later chapters, you will understand how to use the power of protocol-oriented programming to write flexible and easier-to-manage code in Swift. Finally, you will learn how to add the copy-on-write feature to your custom value types, along with understanding how to avoid memory management issues caused by strong reference cycles.

By the end of this Swift book, you will have mastered the Swift 5.3 language and developed the skills you need to effectively use its features to build robust applications.

What you will learn

  • Understand core Swift components, such as operators, collections, control flows, and functions
  • Identify how and when to use classes, structures, and enumerations
  • Use protocol-oriented design with extensions to write easy-to-manage code
  • Leverage design patterns with Swift to solve commonly occurring design problems
  • Apply copy-on-write for your custom value types to improve performance
  • Add concurrency to your applications using Grand Central Dispatch and operation queues
  • Implement generics to write flexible and reusable code

Who this book is for

This book is for beginners with a basic understanding of programming and experienced developers looking to learn Swift programming. Familiarity with Apple's tools will be beneficial but not mandatory. All examples should also work on the Linux and Windows platforms

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Mastering Swift 5.3 by Jon Hoffman in PDF and/or ePUB format, as well as other popular books in Computer Science & Operating Systems. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781800569973
Edition
6

8

Classes, Structures, and Protocols

The first programming language that I learned was BASIC. It was a good language to begin programming with, but once I traded in my Commodore Vic-20 for a PCjr (yes, I had a PCjr and I really enjoyed it), I realized that there were other, more advanced languages out there, and I spent a lot of time learning Pascal and C. It wasn't until I started college that I heard the term object-oriented programming language. At that time, object-oriented programming languages were so new that there were no real courses on them, but I was able to experiment a little with C++. After I graduated, I left object-oriented programming behind, and it really wasn't until several years later, when I started to experiment with C++ again, that I really discovered the power and flexibility of object-oriented programming. In this chapter, we will cover the following topics:
  • What are classes and structures?
  • How to add properties and property observers to classes and structures
  • How to add methods to classes and structures
  • How to add initializers to classes and structures
  • How and when to use access controls
  • How to create a class hierarchy
  • How to extend a class

What are classes and structures?

In Swift, classes and structures are very similar. If we really want to master Swift, it is very important to not only understand what makes classes and structures so similar, but to also understand what sets them apart, because they are the building blocks of our applications. Apple describes them as follows:
Classes and structures are general-purpose, flexible constructs that become the building blocks of your program's code. You define properties and methods to add functionality to your classes and structures by using the already familiar syntax of constants, variables, and functions.
Let's begin by taking a quick look at some of the similarities between classes and structures.

Similarities between classes and structures

In Swift, classes and structures are more similar than they are in other languages, such as Objective-C. The following is a list of some of the features that classes and structures share:
  • Properties: These are used to store information in our classes and structures
  • Methods: These provide functionality for our classes and structures
  • Initializers: These are used when initializing instances of our classes and structures
  • Subscripts: These provide access to values using the subscript syntax
  • Extensions: These help extend both classes and structures
Now, let's take a quick look at some of the differences between classes and structures.

Differences between classes and structures

While classes and structures are very similar, there are also several very important differences. The following is a list of some of the differences between classes and structures in Swift:
  • Type: A structure is a value type, while a class is a reference type
  • Inheritance: A structure cannot inherit from other types, while a class can
  • Deinitializers: Structures cannot have custom deinitializers, while a class can
Throughout this chapter, we will be emphasizing the differences between classes and structures to help us understand when to use each. Before we really dive into classes and structures, let's look at the difference between value types (structures) and reference types (classes). To fully understand when to use classes and structures and how to properly use them, it is important to understand the difference between value and reference types.

Value versus reference types

Structures are value types. When we pass instances of a structure within our application, we pass a copy of the structure and not the original structure. Classes are reference types; therefore, when we pass an instance of a class within our application, a reference to the original instance is passed. It is very important to understand this difference. We will give a very high-level view here and will provide additional details in Chapter 18, Memory Management. When we pass structures within our application, we are passing copies of the structures and not the original structures. Since the function gets its own copy of the structure, it can change it as needed without affecting the original instance of the structure. When we pass an instance of a class within our application, we are passing a reference to the original instance of the class. Since we're passing the instance of the class to the function, the function is getting a reference to the original instance; therefore, any changes made within the function will remain once the function exits. To illustrate the difference between value and reference types, let's look at a real-world object: a book. If we have a friend who wants to read Mastering Swift 5.3, we could either buy them their own copy or share ours. If we bought our friend ...

Table of contents

  1. Preface
  2. Taking the First Steps with Swift
  3. Swift Documentation and Installing Swift
  4. Learning about Variables, Constants, Strings, and Operators
  5. Optional Types
  6. Using Swift Collections
  7. Control Flow
  8. Functions
  9. Classes, Structures, and Protocols
  10. Protocols and Protocol Extensions
  11. Protocol-Oriented Design
  12. Generics
  13. Error Handling and Availability
  14. Custom Subscripting
  15. Working with Closures
  16. Advanced and Custom Operators
  17. Concurrency and Parallelism in Swift
  18. Custom Value Types
  19. Memory Management
  20. Swift Formatting and Style Guide
  21. Adopting Design Patterns in Swift
  22. Other Books You May Enjoy
  23. Index