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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Swift

You're reading from   Mastering Swift

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784392154
Length 358 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 (17) Chapters Close

Preface 1. Taking the First Steps with Swift FREE CHAPTER 2. Learning about Variables, Constants, Strings, and Operators 3. Using Collections and Cocoa Data Types 4. Control Flow and Functions 5. Classes and Structures 6. Working with XML and JSON Data 7. Custom Subscripting 8. Using Optional Type and Optional Chaining 9. Working with Generics 10. Working with Closures 11. Using Mix and Match 12. Concurrency and Parallelism in Swift 13. Swift Formatting and Style Guide 14. Network Development with Swift 15. Adopting Design Patterns in Swift Index

Hello world

All good computer books that are written to teach a computer 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 two different Hello World applications.

Our fist Hello World application will be the 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. The Playground can be either an iOS or an OS X Playground.

In Swift, there are two functions that will print messages to the console. These functions are print() and println(). The print() function will print the textural representation of the object passed to it, while the println() function will also print the textural representation of the object passed to it, and will also add a new line character to the end.

The println() function is overloaded to receive either an object to print or no value, in which case, we will print only a newline character to the console.

Let's add the println() function, as shown in the following Playground, to our Playground to print the Hello World message to the console. We will then need to open the Timeline sidebar (keyboard shortcut: Command-option and Enter) to see what is being printed to the console. Our Playground should look like the following Playground once we are done:

Hello world

As we can see in the Timeline sidebar, our application will print the message Hello World! to the console.

Now, let's see if we can spice this up a bit and include a variable. We will be covering variables a lot more in the next chapter, so, for right now, it is alright if you do not fully understand how variables in Swift work; we mainly want to show you how to include variables in a println() function.

There are two ways in which we can add a string to another string. We can concatenate them or we can include them inline. To concatenate two strings, we use the + operator to add them together. The following code is an example of concatenating two strings:

var stringC = stringA + stringB

To include a string inline, we use a special sequence of characters \( ). The following code shows how to include a string inline with another string:

var name = "Jon"
var stringC = "Hello \(name)"

The following Playground shows examples of how to concatenate strings and how to include strings inline:

Hello world

For the message1 variable, we concatenate the strings together, while for the message2 variable, we include the other strings inline. We then use the prinln() function to display both messages to the console.

You have been reading a chapter from
Mastering Swift
Published in: Jun 2015
Publisher: Packt
ISBN-13: 9781784392154
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