Book Image

Mastering Swift 5.3 - Sixth Edition

By : Jon Hoffman
Book Image

Mastering Swift 5.3 - Sixth Edition

By: Jon Hoffman

Overview of this book

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.
Table of Contents (23 chapters)
21
Other Books You May Enjoy
22
Index

Hello World

All good computer books that are written to teach a computer language have a section that shows the user how to write a Hello World application. This book is no exception. In this section, we will show you how to write two different Hello World applications.

Our first Hello World application will be a traditional Hello World application that simply prints Hello World to the console. Let's begin by creating a new playground and naming it Chapter_1_Hello_World.

In Swift, to print a message to the console, we use the print() function. In its most basic form, we would use the print() function to print out a single message, as shown in the following code:

print("Hello World")

Usually, when we use the print() function, we want to print more than just static text. We can include the value of variables and/or constants by using string interpolation or by separating the values within the print() function with commas. String interpolation uses a special sequence of characters, \( ), to include the values of variables and/or constants in the string. The following code shows how to do this:

let name = "Jon"
let language = "Swift"
var message1 = " Welcome to the wonderful world of "
var message2 = "\(name), Welcome to the wonderful world of \(language)!"
print(message2)
print(name, message1, language, "!")

We can also define two parameters in the print() function that change how the message is displayed in the console. These parameters are the separator and terminator parameters. The separator parameter defines a string that is used to separate the values of the variables/constants in the print() function. By default, the print() function separates each variable/constant with a space. The terminator parameter defines what character is put at the end of the line. By default, the newline character is added at the end of the line.

The following code shows how we would create a comma-separated list that does not have a newline character at the end:

let name1 = "Jon" 
let name2 = "Kailey" 
let name3 = "Kara"
print(name1, name2, name3, separator:", ", terminator:"")

There is one other parameter that we can add to our print() function: the to: parameter. This parameter will let us redirect the output of the print() function. In the following example, we redirect the output to a variable named line:

let name1 = "Jon" 
let name2 = "Kailey" 
let name3 = "Kara"
var line = ""
print(name1, name2, name3, separator:", ", terminator:"", to:&line) 
print(line)

Previously, the print() function was simply a useful tool for basic debugging, but now, with the new, enhanced print() function, we can use it for a lot more.

The output from the previous two examples is a comma-separated list of Jon, Kailey, Kara.