Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Mastering Swift 5.3
Mastering Swift 5.3

Mastering Swift 5.3: Upgrade your knowledge and become an expert in the latest version of the Swift programming language , Sixth Edition

eBook
$17.99 $26.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Mastering Swift 5.3

Swift Documentation and Installing Swift

I spent a lot of my career working as a Linux System Administrator and Network Security Administrator. These positions required me to compile and install packages from source. There are a lot of advantages to building packages from source rather than downloading prebuilt binaries. The biggest advantage, in my opinion, is that you can get the latest version without having to wait for someone else to build it. This allows me to patch my systems with the latest security updates without delay. With Swift, we are also able to download the latest code and compile ourselves without having to wait for someone else to build it.

In this chapter, you will learn:

  • About the swift.org website and what it offers
  • How to find the latest documentation on Swift
  • Ways to install Swift
  • How to build Swift from source with its full toolchain and package manager

In the previous chapter, we mentioned that Apple has released...

Swift.org

On December 3, 2015, Apple officially released the Swift language, supporting libraries, the debugger, and the package manager to the open source community under the Apache 2.0 license. At that time, the swift.org site was created as the community's gateway to the project. This site has a wealth of information and should be your primary site to find out what is happening in the Swift community and the language itself. The blog posts will keep you up to date with new releases of Swift, new Swift open source libraries, changes to the standard library, and other Swift news.

You can also download pre-built binaries for several flavors of Linux. At the time this book is being written, we can download pre-built binaries for Ubuntu 16.04, Ubuntu 18.04, Ubuntu 20.04, CentOS 8, and Amazon Linux 2. The getting started page provides a list of dependencies for the previously mentioned flavors of Linux and instructions on how to install the binaries.

The website...

Swift documentation

Apple and the Swift community, as a whole, have released a number of resources to help developers program in Swift. Apple's official documentation, which can be found at https://developer.apple.com/documentation/, includes API documentation for Swift as well as all of Apple's frameworks. Only a small portion of Apple's frameworks are open source and work across all platforms; however if you are looking to get started on one of Apple's frameworks this is definitely the place to start. However it can be hard to find documentation on specific Swift APIs.

To quickly find documentation on Swift's API, my favorite site is https://swiftdoc.org. This site is incredibly easy to navigate and has autogenerated documentation for all types, protocols, operators, and global functions that make up the Swift language. I have noticed that this site does not always stay up to date; however, it is a great reference for any Swift developer. The code that...

Installing Swift from swift.org

If you are developing on and for the Apple platform, I strongly suggest that you stick with the version of Swift that comes with Xcode. Apple will not approve an app that was compiled using a version of Swift that is different than the one that came with the version of Xcode that you are using. This may seem a little extreme, but it ensures that the apps are compiled with a stable version of Swift and one that has been fully vetted to work with your version of Xcode.

If you are using one of the flavors of Linux that has pre-built binaries on the swift.org site, it is recommended that you use those. They are the easiest and quickest path to getting Swift up and running. You can also find complete installation instructions with a list of dependencies on the swift.org site under the Getting Started section.

If pre-built binaries are not provided for your flavor of Linux, if you want to try the latest version of Swift, or if you just want...

Building Swift and the Swift toolchain from source

There are a number of sites that show you how to build Swift from source, but unfortunately most of these sites give directions for building just the Swift language itself without the toolchain. I find that not very useful, unless you are only writing very simple applications. In my opinion, building Swift for Linux without the entire toolchain and the package manager is more of an exercise of "Can I do it" rather than building something you can use long term.

While using the latest build of Swift is not recommended for production systems, it does enable us to use the latest features of the language and also verify that changes introduced in our application are compatible with future versions of the Swift language.

In this chapter, we will look at how we can build Swift, its entire toolchain, and the Swift package manager from source. Since each flavor of Linux and macOS are somewhat different, I need...

Using the Swift package manager

There is a lot that you can do with the package manager that makes it a necessity for creating complex applications on the Linux platform. It helps with adding dependencies to projects and enables us to break our code up into multiple files and create library projects. You can use the package manager on the Mac platform as well, but I do find it easier to use Xcode.

For the examples in this book, we will not need to add dependencies or use multiple files. Let's see how we can simply build and run an executable project in the package manager so you can use it to run the examples from this book if you would like. Keep in mind you are able to use the package manager on the Apple platform as well. When the package manager created main.swift in the Sources/test/ directory it added the following code to it:

print("Hello, world!")

This code gives us the basic Hello World application. You can replace this code with examples from...

Using the Swift compiler

The Swift compiler is the basic utility to build Swift code and it is used by the package manager, Xcode, and any other utility that builds Swift code into executables. We can also call it ourselves. To see how to call it ourselves, create a file named hello.swift and add the print("Hello, world!") code to it as shown with the following code:

echo `print("Hello, world!")` >> hello.swift

Now we can compile this code with the following command, which calls the Swift compiler:

swiftc hello.swift

Finally we can execute the newly created application like we would any other executable:

./hello

And we will be greeted with our Hello, world! message.

Summary

In this chapter, we looked at some of the different documentation that Apple and the Swift community offer. This documentation can be essential as you are learning Swift and also for reference once you have mastered the language itself. We also looked at how we can build and install Swift and its full toolchain. While using the latest build of Swift is not recommended for production systems, I usually keep a virtual machine or my desktop setup with a recent build. This enables me to use the latest features of the language and also run my code against it to make sure I am not introducing changes that are not compatible with future versions of the language.

In the next chapter, we will start to get into the language itself and we will see how to use variables and constants in Swift. We will also look at the various data types and how to use operators in Swift.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

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.

Who is this book 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

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 23, 2020
Length: 418 pages
Edition : 6th
Language : English
ISBN-13 : 9781800562158
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 23, 2020
Length: 418 pages
Edition : 6th
Language : English
ISBN-13 : 9781800562158
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 132.97
iOS 14 Programming for Beginners
$54.99
SwiftUI Projects
$38.99
Mastering Swift 5.3
$38.99
Total $ 132.97 Stars icon

Table of Contents

22 Chapters
Taking the First Steps with Swift Chevron down icon Chevron up icon
Swift Documentation and Installing Swift Chevron down icon Chevron up icon
Learning about Variables, Constants, Strings, and Operators Chevron down icon Chevron up icon
Optional Types Chevron down icon Chevron up icon
Using Swift Collections Chevron down icon Chevron up icon
Control Flow Chevron down icon Chevron up icon
Functions Chevron down icon Chevron up icon
Classes, Structures, and Protocols Chevron down icon Chevron up icon
Protocols and Protocol Extensions Chevron down icon Chevron up icon
Protocol-Oriented Design Chevron down icon Chevron up icon
Generics Chevron down icon Chevron up icon
Error Handling and Availability Chevron down icon Chevron up icon
Custom Subscripting Chevron down icon Chevron up icon
Working with Closures Chevron down icon Chevron up icon
Advanced and Custom Operators Chevron down icon Chevron up icon
Concurrency and Parallelism in Swift Chevron down icon Chevron up icon
Custom Value Types Chevron down icon Chevron up icon
Memory Management Chevron down icon Chevron up icon
Swift Formatting and Style Guide Chevron down icon Chevron up icon
Adopting Design Patterns in Swift Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(15 Ratings)
5 star 33.3%
4 star 33.3%
3 star 13.3%
2 star 13.3%
1 star 6.7%
Filter icon Filter
Top Reviews

Filter reviews by




Nicholas Jan 15, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I purchased the previous version of this book with a humble bundle a while back so this review will take a moment to focus on whats new in the 6th ed:A chapter about Swift documentation / installing swift was inserted and is now chapter 2. This will help you understand how to work with swift on Linux if you dont have a Mac, HOWEVER sticking to XCode on Mac is the optimal experience.Also new in this verson: error handling gets an added section on multi pattern catch clauses which is new to swift 5.3 and a good addition by the author.Synthesized Comparable conformance for enumerations is new to 5.3 and is also briefly covered in this book.A new chapter on Custom Operators is in this edition. I feel like this chapter is geared toward really experienced developers because it was a bit advanced for where I am in my journey.On the book itself. You dont really need much if any pre-existing knowledge on the topic before you start with this. The book has a chapter on almost every topic ranging from variables, collection types, control flow, to functions and more. The book takes time to explain differences between swift and other languages (such as semi-colons or assignment operators) so that is helpful if you are coming from that background. Towards the end he provides a style/formatting guide which will help you keep your code looking professional when its reviewed by potential employers. The design patterns chapter is cool but every employer has their own design pattern so dont waste your time memorizing these, a general understanding is more than enough.As a final note I would like to say this book is really good at teaching you the concepts of Swift as it stands in 5.3, with example code in the book done in playgrounds. After this book I suggest following along with app building tutorials on youtube to help you build your practical skillset and show you how to apply the really great concepts you get from this book. It can be a little dry reading at times but its straight to the point and I highly recommend reading this if youre trying to really get a grip on the language.
Amazon Verified review Amazon
Jay Jay Jan 11, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Thanks
Amazon Verified review Amazon
Andre P. Dec 09, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
"Mastering Swift" has been my to go resource for Swift from its First Edition. The brand new Sixth Edition contains all the relevant updates for Swift 5.3, which allows iOS developers to easily keep up with the language evolution. My favorite chapters are "Concurrency and Parallelism in Swift", which focuses on GCD, and "Adopting Design Patterns in Swift".Definitely a great addition to any programming books collection!
Amazon Verified review Amazon
Richard Albury Dec 13, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is a pleasant surprise. I breezed through the book in a week, and despite a few years of Objective-C and Swift experience, I still learned a few things.It does a great job of hitting the important aspects of the language while also providing good context, so it’s not just a collection of dry pedagogical exercises. The chapter on swift.org and building Swift is a great introduction to one of Swift’s hidden strengths: an enthusiastic and diverse community of people who care about this open, developer-driven language and the platforms it runs on.I also have to call out the chapter on closures: it’s one of the better introductions to a topic a lot of beginning Swift developers struggle with. The chapter on concurrency deserves mention as well: these two topics are fundamental to master, and the book does an exceptional job with them.Finally, the ending chapter on design patterns provides a good grounding in an advanced topic.Highly recommended. If you had to buy a single book on Swift, this would be it.
Amazon Verified review Amazon
Just Me Dec 07, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have purchased a copy of almost all of the Mastering Swift books because I find them incredibly informative. The author does a great job of teaching Swift while also covering the most important new features of the language. While the first few chapters are geared towards someone first learning the language, the later chapters are a must read for anyone that wants to master the Swift Language. From Protocol Oriented Programming techniques and Copy-on-Write to Concurrency and Generics, this book covers almost all of the advanced topicsWhen I first received my copy of this book, I went right to the Advanced Operators chapters which is one of the two new chapters added in this revision. This is one of the few books that cover details like the Bitwise operators and custom operators. This chapter is a great addition to the book.Just like previous revisions of this book, I consider this a most read for beginners to the Swift Language and also for experience developers that want to take their knowledge to the next level.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.