Reader small image

You're reading from  Swift Cookbook - Second Edition

Product typeBook
Published inFeb 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781839211195
Edition2nd Edition
Languages
Tools
Right arrow
Authors (2):
Keith Moon
Keith Moon
author image
Keith Moon

Keith Moon is an award-winning iOS developer, author and speaker based in London. He has worked with some of the biggest companies in the world to create engaging and personal mobile experiences. Keith has been developing in Swift since its release, working on projects both fully Swift, and mixed Swift and Objective-C. Keith has been invited to speak about Swift development in conferences from Moscow to Minsk and London.
Read more about Keith Moon

Chris Barker
Chris Barker
author image
Chris Barker

Chris Barker is an iOS developer and tech lead for fashion retailer N Brown (JD Williams, SimplyBe, Jacamo), where he heads up the iOS team. Chris started his career developing .NET applications for online retailer dabs (now BT Shop) before he made his move into mobile app development with digital agency Openshadow (now MyStudioFactory Paris). There, he worked on mobile apps for clients such as Louis Vuitton, L'Oréal Paris, and the Paris Metro. Chris often attends and speaks at local iOS developer meetups and conferences such as NSManchester, Malaga Mobile, and CodeMobile.
Read more about Chris Barker

View More author details
Right arrow
Preface

Since Apple announced the Swift programming language at WWDC 2014, it has gone on to become one of the fastest-growing programming languages. Swift is modern, open-source, and easy to use, and therefore its usefulness can extend beyond Apple's ecosystem, giving it the potential to be used across all platforms and for any scenario.

Swift 5.3 is the latest version of this exciting new programming language, giving you the tools to build performant and responsive apps with safe and clean code.

This book will guide you through Swift's features, building up your knowledge and toolset layer by layer, so you can use Swift to build the next great app or service.

You will be given useful, easy-to-follow recipes for using Swift to accomplish real-world tasks. Each recipe only uses concepts previously covered in the book so you will never feel lost.

Learn what makes Swift one of the fastest-growing and most exciting programming languages available today.

Who this book is for

If you are looking for a book to help you learn about the diverse features offered by Swift 5, along with tips and tricks to efficiently code and build applications, then this book is for you. Knowledge of Swift or general programming concepts will be beneficial.

What this book covers

Chapter 1, Swift Building Blocks, introduces you to the basic building blocks of Swift 5, its syntax, and the functionalities of basic Swift constructs. Also, this chapter will introduce you to Apple’s Xcode IDE and Swift Playgrounds, which provides an ideal way to create, execute, debug, and understand the recipes contained in this book, thus setting you up to initiate the development process. In this chapter, you will learn how to write your first Swift program and understand the various basic elements of the Swift language.

Chapter 2, Mastering the Building Blocks, teaches you how to create more complex structures on the basis of the building blocks that you studied in the first chapter and the functionalities provided by the Swift standard library. You will get an understanding of how to bundle variables into tuples, order data with the help of an array, and store key-value pairs with dictionaries. You will also learn how to use the property observers and control the access to and visibility of your code. Then, you will learn how to extend the functionalities of your code using the extensions.

Chapter 3, Data Wrangling with Swift Control Flow, will teach you that programming is all about making decisions, therefore in this chapter, we will explore how to make decisions on the basis of the information gained and how to alter the control flow of the code. You will learn how to conditionally execute your code with if/else statements. You will also learn how to control the flow of execution of your code with switch statements, and then loop this code by understanding how to use for and while loops. You will then understand how to handle Swift errors with try, throw, do, and catch statements. Also, we will cover how a defer statement can be useful to change state once a function's execution is complete or to clean up values that are no longer needed.

Chapter 4, Generics, Operators, and Nested Types, provides you with an understanding of two advanced features of Swift, which are generics and operators. Using these features, you will learn how to build functionalities that are flexible and well defined. Also, you will understand how nested types allow logical grouping, access, and namespacing for your constructs.

Chapter 5, Beyond the Standard Library, takes you on a journey to explore the functionalities beyond the standard library provided by frameworks such as Foundation and UIKit. Learning how to use these functionalities will help you make full use of the Swift language.

Chapter 6, Building iOS Apps with Swift, is where we'll start the journey into building our very own iOS app. Taking everything we've learned and more from the previous chapters' recipes, this chapter will teach you how to get off the ground using Swift to build an iOS app.

Chapter 7, Swift Playgrounds, is where you will gain a total understanding of using Swift Playgrounds and explore advanced features other than those explored in previous chapters, to create fully interactive experiences.

Chapter 8, Server-Side Swift, introduces you to a totally different aspect of Swift programming: server-side programming with Swift. Also, you will gain an understanding of how to run Swift on Linux by installing the Swift toolchain. You will learn how to use a web server framework to build a REST API and how to host your API via a hosting service. You will also learn how to accomplish your tasks easily by understanding how to use Vapor, one of the most popular frameworks of Swift 5.

Chapter 9, Performance and Responsiveness in Swift, explores the more advanced concepts of Swift programming to provide an understanding of how certain Swift types are implemented and their performance characteristics. Also, you will learn how to perform asynchronous tasks using the Grand Central Dispatch API. Then, we'll explore the multi-threaded environment available on all Apple platforms and how to enhance the performance profile of your Swift constructs to build a fast and responsive app.

Chapter 10, SwiftUI and Combine Framework, dives straight into Apple's new UI framework, SwiftUI. With SwiftUI, we'll learn how to create iOS apps by exploring the declarative syntax used to achieve this. We'll also explore how easy it is for SwiftUI and UIKit to work together. In addition to SwiftUI, we'll also explore the basics of the new Combine framework and how this works alongside SwiftUI.

Chapter 11, Using CoreML and Vision in Swift, will dive into the frameworks offered by Apple and how we can use Swift code to harness machine learning techniques, as machine learning is becoming an increasingly hot topic. In this chapter, we'll cover CoreML and the Vision frameworks, using pre-trained models to identify objects in real time.

To get the most out of this book

To follow along with the examples in this book, you will need a computer running macOS Catalina (specifically version 10.15.4) or greater. You also need an Apple ID to download and install Xcode 12 from the Mac App Store. The chapter on server-side Swift (Chapter 8, Server-Side Swift) also requires a system running Ubuntu 20.04 LTS.

The code in this book has been tested against Swift 5.3, but should work with any newer versions of Swift.

Software/hardware covered in the book

OS requirements

macOS 10.15.4+ (Catalina)
Xcode 12+
Ubuntu (for Chapter 8, Server-Side Swift) 20.04 LTS

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Swift-Cookbook-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Code in Action

Code in Action videos for this book can be viewed at http://bit.ly/3shdTeQ.

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781839211195_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We can substitute Grumble wherever we would use [Pug] or Array<Pug>."

A block of code is set as follows:

let fraction = rating / total 
let ratingOutOf5 = fraction * 5
let roundedRating = round(ratingOutOf5) // Rounds to the nearest
// integer.

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

class ProgrammeFetcher { 

typealias FetchResultHandler = (String?, Error?) -> Void

func fetchCurrentProgrammeName(forChannel channel: Channel,
resultHandler: FetchResultHandler) {
// Get next programme
let programmeName = "Sherlock"
resultHandler(programmeName, nil)
}

Any command-line input or output is written as follows:

$ mkdir css
$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Swift Cookbook - Second Edition
Published in: Feb 2021Publisher: PacktISBN-13: 9781839211195
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

Authors (2)

author image
Keith Moon

Keith Moon is an award-winning iOS developer, author and speaker based in London. He has worked with some of the biggest companies in the world to create engaging and personal mobile experiences. Keith has been developing in Swift since its release, working on projects both fully Swift, and mixed Swift and Objective-C. Keith has been invited to speak about Swift development in conferences from Moscow to Minsk and London.
Read more about Keith Moon

author image
Chris Barker

Chris Barker is an iOS developer and tech lead for fashion retailer N Brown (JD Williams, SimplyBe, Jacamo), where he heads up the iOS team. Chris started his career developing .NET applications for online retailer dabs (now BT Shop) before he made his move into mobile app development with digital agency Openshadow (now MyStudioFactory Paris). There, he worked on mobile apps for clients such as Louis Vuitton, L'Oréal Paris, and the Paris Metro. Chris often attends and speaks at local iOS developer meetups and conferences such as NSManchester, Malaga Mobile, and CodeMobile.
Read more about Chris Barker