Book Image

Mastering Swift 2

By : Jon Hoffman
Book Image

Mastering Swift 2

By: Jon Hoffman

Overview of this book

<p><span id="description" class="sugar_field">At their Worldwide Developer’s conference (WWDC) in 2015, Apple announced Swift 2, a major update to the innovative programming language they first unveiled to the world the year before. Swift 2 features exciting enhancements to the original iteration of Swift, acting, as Apple put it themselves as “a successor to the C and Objective-C languages.” – This book demonstrates how to get the most from these new features, and gives you the skills and knowledge you need to develop dynamic iOS and OS X applications.<br /> </span></p> <p><span id="description" class="sugar_field">Learn how to harness the newest features of Swift 2 todevelop advanced applications on a wide range of platforms with this cutting-edge development guide. Exploring and demonstrating how to tackle advanced topics such as Objective-C interoperability, ARC, closures, and concurrency, you’ll develop your Swift expertise and become even more fluent in this vital and innovative language. With examples that demonstrate how to put the concepts into practice, and design patterns and best practices, you’ll be writing better iOS and OSX applications in with a new level of sophistication and control.</span></p>
Table of Contents (24 chapters)
Mastering Swift 2
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Free Chapter
1
Taking the First Steps with Swift
2
Learning about Variables, Constants, Strings, and Operators
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, to print a message to the console, we use the print() function. The print() function has been greatly enhanced in Swift 2. Prior to Swift 2, we had two separate print functions: print() and println(). Now both of these functions have been combined into the single print() function.

In the most basic form, to print out a single message, we would use the print function 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)

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/constant 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. 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:"", toStream:&line)

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