-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Swift 4 Programming Cookbook

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 Swift's usefulness can extend beyond Apple's ecosystem, giving it the potential to be used across all platforms and for any scenario.
Swift 4 represents 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.
Chapter 1,Swift Building Blocks, introduces you to the basic building blocks of Swift 4, its syntax, and the functionalities of basic Swift constructs. Also, this chapter will introduce you to Apple’s Xcode 9 IDE and Swift Playgrounds, which provide 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, learn to write your first Swift program and understand the various basic elements of the Swift language.
Chapter 2,Building on the Building Blocks, teaches the reader 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 the variables into tuples, order the data with the help of an array, and store key-value pairs with Dictionaries. Also, you can learn to use the property observers and control the access to and visibility of your code. Then, you will also learn to extend the functionalities of your codes using the extensions.
Chapter 3, Data Wrangling with Swift Control Flow, says that programming is all about making decisions; therefore, this chapter explores how to make a decision on the basis of information gained and how to alter the control flow of the code. You can learn how to conditionally execute code with if
/else
statements, control the flow of execution of your code with switch
statements, and then loop the code by understanding how to use the for
and while
loops. Then, you will understand how to handle Swift errors with the try
, throw
, do
, and catch
statements, and also 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 of the advanced features of Swift, which are generics and operators. Using these features, you can learn to build functionalities that are flexible and well defined, and also 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 to use these functionalities will help you make full use of the Swift language.
Chapter 6, Swift Playgrounds, gives a total understanding of using Swift Playgrounds and explores advanced features, apart from those explored in the initial chapters, to create fully interactive experiences.
Chapter 7,Server-Side Swift, covers a totally different aspect of Swift programming, server-side programming with Swift. Also, you can gain an understanding of how to run Swift on Linux by installing the Swift toolchain, learn to use a web server framework to build a REST API, and host your API via a hosting service. Also, you can learn to accomplish your tasks easily by understanding how to use Vapor, one of the most popular frameworks in Swift 4.
Chapter 8, Performance and Responsiveness in Swift, concludes the book by exploring the more advanced concepts of Swift programming to gain an understanding of how certain Swift types are implemented and their performance characteristics. Also, it explains how to perform asynchronous tasks using Grand Central Dispatch. Then, it explores the multithreaded environment available on all Apple platforms and how to enhance the performance profile of your Swift constructs to build a fast and responsive app.
To follow along with the examples in this book, you will need a computer running macOS 10.12.6 or greater. You also need an Apple ID to download and install Xcode 9 from the Mac App Store. The chapter on server-side Swift also requires Ubuntu 16.04 LTS.
If you are looking for a book to help you learn about the diverse features offered by Swift 4, along with tips and tricks to efficiently code and build applications, then this book is for you. Basic knowledge of Swift or general programming concepts will be beneficial.
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, we use these sections as follows:
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.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:
"Next, we'll create the method that will take an Int
and return PoolBallType
".
A block of code is set as follows:
func poolBallType(forNumber number: Int) -> PoolBallType { if number < 8 { return .solid } else if number > 8 { return .stripe } else { return .black } }
Any command-line input or output is written as follows:
brew install postgres
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The repository landing page provides the structure and location of JSON configuration file under Configure PostgreSQL".
Warnings or important notes appear like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected]
, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors .
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can download the code files by following these steps:
SUPPORT
tab at the top.Code Downloads & Errata
.Search
box.Code Download
.You can also download the code files by clicking on the Code Files
button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search
box. Please note that you need to be logged in to your Packt account. Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code for the book is also hosted on GitHub at https://github.com/PacktPublishing/Swift-4-Programming-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Swift4ProgrammingCookbook_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form
link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata
section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected]
with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected]
, and we will do our best to address the problem.
Change the font size
Change margin width
Change background colour