Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Swift 3 - Linux

You're reading from   Mastering Swift 3 - Linux Learn to build fast and robust applications on the Linux platform with Swift

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786461414
Length 380 pages
Edition 1st 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 (24) Chapters Close

Mastering Swift 3 - Linux
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Taking the First Steps with Swift FREE CHAPTER 2. Learning About Variables, Constants, Strings, and Operators 3. Using Swift Collections and the Tuple Type 4. Control Flow and Functions 5. Classes and Structures 6. Using Protocols and Protocol Extensions 7. Protocol-Oriented Design 8. Writing Safer Code with Error Handling 9. Custom Subscripting 10. Using Optional Types 11. Working with Generics 12. Working with Closures 13. Using C Libraries with Swift 14. Concurrency and Parallelism in Swift 15. Swifts Core Libraries 16. Swift on Single Board Computers 17. Swift Formatting and Style Guide 18. Adopting Design Patterns in Swift

Hello World


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

Let's begin by creating a new file named main.swift. The main.swift file is a special file in Swift and is the entry point for our application. It is the only file that can contain top-level code. Top-level code is the code that is not part of a function or type (enumeration, class, or structure). All of the code for our Hello World application is considered top-level code.

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 using a special sequence of characters, \( ), or by separating the values within the print() function with commas. 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(name, message1, language, "!") 
print(message2) 

In order to compile this code, it will need to be in a file name main.swift. You can use any text editor, like emacs or VI, to create this file. Once the main.swift file is created, we will need to build our Hello World application. Type the following line in the same directory where the main.swift file is located:

    swiftc main.swift

Once the swift compiler finishes building the application, we will have an executable file named main in our directory. We can run the file using the following command:

    ./main 

If all goes well, you can see the following output:

Jon Welcome to the wonderful world of Swift!
Jon Welcome to the wonderful world of Swift!

We will look at the swift and swiftc commands later in this chapter.

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 variable/constant in the print() function. By default, the print() function separates each variable/constant with a space. The terminator parameter defines which character is inserted 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 could 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. This is the toStream 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) 

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

You have been reading a chapter from
Mastering Swift 3 - Linux
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781786461414
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 £13.99/month. Cancel anytime
Visually different images