Book Image

Swift 4 Programming Cookbook

Book Image

Swift 4 Programming Cookbook

Overview of this book

Swift 4 is an exciting, multi-platform, general-purpose programming language. Being open source, modern and easy to use has made Swift one of the fastest growing programming languages. If you interested in exploring it, then this book is what you need. The book begins with an introduction to the basic building blocks of Swift 4, its syntax and the functionalities of Swift constructs. Then, introduces you to Apple's Xcode 9 IDE and Swift Playgrounds, which provide an ideal platform to write, execute, and debug the codes thus initiating your development process. Next, you'll learn to bundle variables into tuples, set order to your data with an array, store key-value pairs with dictionaries and you'll learn how to use the property observers. Later, explore the decision-making and control structures in Swift and learn how to handle errors in Swift 4. Then you'll, examine the advanced features of Swift, generics and operators, and then explore the functionalities outside of the standard library, provided by frameworks such as Foundation and UIKit. Also, you'll explore advanced features of Swift Playgrounds. At the end of the book, you'll learn server-side programming aspect of Swift 4 and see how to run Swift on Linux and then investigate Vapor, one of the most popular server-side frameworks for Swift.
Table of Contents (9 chapters)

Your first Swift program

In the first recipe, we will get up and running with Swift using a Swift Playground, and we will run our first piece of Swift code.

Getting ready

To run our first Swift program, we need to download and install our IDE. During the beta of Apple's Xcode 9, it is available as a direct download from Apple's developer website at, access to this beta will require a free Apple developer account. Once the beta has ended and Xcode 9 is publicly available, it will also be available from the Mac App Store. By obtaining it from the Mac App Store, you will be informed of updates automatically, so this is the preferred route once Xcode 9 is out of beta.

Downloading Xcode

Follow these steps to download Xcode from the Mac App Store:

  1. Open up the Mac App Store, either from the dock or via Spotlight:
  1. Search for xcode:
  1. Click on Install:
Xcode is a large download (over 4 GB). So, depending on your internet connection, this can take a while.
  1. The progress can be monitored from Launchpad:

Follow these steps to get Xcode as a direct download:

  1. Go to the Apple Developer download page at
  1. Click on the Download button to download Xcode within a .xip file:
  1. Double-click on the downloaded file to unpack the Xcode application.
  1. Drag the Xcode application into your Applications folder:

How to do it...

With Xcode downloaded, let's create our first Swift playground:

  1. Launch Xcode from the icon in your dock.
  2. From the welcome screen, choose Get started with a playground:
  1. From the template chooser, select the blank template from the iOS tab:
  1. Choose a name for your playground and a location to save it:
Xcode Playgrounds can be based on one of the three different Apple platforms: iOS, tvOS, and macOS (the operating system formerly known as OSX). Playgrounds provide full access to the frameworks available to either iOS, tvOS, or macOS, depending on which you choose. An iOS playground will be assumed for the entirety of this book, chiefly because this is the platform of choice of the author. Where recipes do have UI components, the iOS platform will be used until stated otherwise.
  1. You are now presented with a view that looks like this:
  1. Let's replace the word playground with Swift!.
  2. Click on the blue play icon in the bottom left-hand corner of the window to execute the code in the playground:
  1. Congratulations! You have just run some Swift code.
  1. On the right-hand side of the window, you will see the output of each line of code in the playground. We can see that our line of code has output "Hello, Swift!":

There's more...

If you put your cursor over the output on the right-hand side, you will see two buttons: one that looks like an eye and another that is a rounded square:

Click on the eye button to get a Quick Look box of the output. This isn't that useful for just a string, but can be useful for more visual output, such as colors and views:

Click on the square button, and a box will be added in-line, under your code, showing the output of the code. This can be really useful if you want to see how the output changes as you change the code:

See also

We will learn more about playgrounds and how we can take them further in Chapter 6, Swift Playgrounds.