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
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
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
Estimated delivery fee Deliver to Italy

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Estimated delivery fee Deliver to Italy

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

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
€18.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
€189.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
€264.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 101.97
iOS 14 Programming for Beginners
€41.99
SwiftUI Projects
€29.99
Mastering Swift 5.3
€29.99
Total 101.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

Most Recent
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
Most Recent

Filter reviews by




Sayed Jul 30, 2023
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is a excellent book. The reason I give 1 less star them 5 is it’s doesn’t have any thing new to offer to already experienced developer.
Amazon Verified review Amazon
Kim K Feb 13, 2023
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The organization of topics was quite good, as well as explanations, descriptions, and examples illustrating the concepts of Swift. There are nice chapters on extralingual topics such as design patterns and protocal-oriented programming.However, I had many issues with the text:* It is littered with errors, both in the text and in the code. For example the phrase "... throws an error" was used (which is a runtime event), where "... causes a compile-time error" should have been used. And a typical spelling error: "Warth" appears where "Earth" should be. The pages are filled with these kinds of errors.* The index is *very* short, compared to other language books I have. In a reference book, a good index is critical.* In the String section there was no coverage of the Substring struct, which deserves discussion because it does not occur in some other languages.* Example code blocks have comments describing the results. Then paragraphically, the *exact* same thing is said. It is redundant (and painful).* Oddities abound. For example, for enums, he first says it is preferred to begin enum values with lowercase. Then he proceeds to define most examples with uppercase!?* There are fullscreen images of the Xcode editor to show code and run time results, but the font is too small to read!! This occurs frequently in chapter 1, but thankfully less so in the rest of the book. Actually, Swift is available on Linux and Windows, so I would prefer a platform-neutral book, using comments and text to explain the concepts, instead of utilizing the Apple-only Xcode tool.* There are so many of the "next we'll look at xxx yyy", followed immediately with a section title of "Xxx Yyyy". Some may like that, but to me it is just noise.This could be a *great* book, if efforts are made to improve these things.
Amazon Verified review Amazon
Armen Oct 13, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Great reference book, but definitely not for individuals just getting started in programming.
Amazon Verified review Amazon
TxnQ May 22, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is a clear, concise and easily readable presentation of the basics of Swift, certainly usable by anyone familiar with the basics of modern computer-languages. (It is, after all, a description of Swift, not an introductory CS course.)The only problem is that the index (of only 8 pages) is useless for finding anything other than the most rudimentary items. For example, there is no entry for the key word "mutating." This is a situation I've found to be common with books published by "Packt>".Complete answers have to be done with Internet searches, or (as is perhaps the goal of such under-documentation) by subscribing to packtpub.com.
Amazon Verified review Amazon
David Jan 09, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Looking for a book underwriting the more intermediate and advanced approaches to programming with Swift, I was initially concerned with the beginner-level difficulty of first few chapters of actual Programming content. However, I was even surprised the odd piece of Swift functionality in these first chapters that had not as yet come across.My concerns weren’t long-lived. The book, although not in great depth, covers a large variety of programming paradigms and concepts and how they are approached with, Swift 5. Overall, I’m glad I bought this book.
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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela