Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Swift 5

You're reading from   Mastering Swift 5 Deep dive into the latest edition of the Swift programming language

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781789139860
Length 370 pages
Edition 5th Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jon Hoffman Jon Hoffman
Author Profile Icon Jon Hoffman
Jon Hoffman
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Taking the First Steps with Swift FREE CHAPTER 2. Learning about Variables, Constants, Strings, and Operators 3. Optional Types 4. Using Swift Collections 5. Control Flow 6. Functions 7. Classes, Structures, and Protocols 8. Using Protocols and Protocol Extensions 9. Protocol Oriented Design 10. Generics 11. Availability and Error Handling 12. Custom Subscripting 13. Working with Closures 14. Concurrency and Parallelism in Swift 15. Custom Types 16. Memory Management 17. Swift Formatting and Style Guider 18. Adopting Design Patterns in Swift 19. Other Books You May Enjoy

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 value of variables and/or constants in the string. The following code shows how to do this:

var name = "Jon" 
var 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:

var name1 = "Jon"  
var name2 = "Kim"  
var name3 = "Kailey"  
var name4 = "Kara" 
 
print(name1, name2, name3, name4, 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:

var name1 = "Jon" 
var name2 = "Kim" 
var name3 = "Kailey" 
var name4 = "Kara" 
 
var line = "" 
 
print(name1, name2, name3, name4, 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.

You have been reading a chapter from
Mastering Swift 5 - Fifth Edition
Published in: Apr 2019
Publisher: Packt
ISBN-13: 9781789139860
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.
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 $19.99/month. Cancel anytime
Banner background image