Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Swift 3 New Features

You're reading from  Swift 3 New Features

Product type Book
Published in Oct 2016
Publisher Packt
ISBN-13 9781786469632
Pages 142 pages
Edition 1st Edition
Languages
Author (1):
Keith Elliott Keith Elliott
Profile icon Keith Elliott

Table of Contents (16) Chapters

Swift 3 New Features
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. What Were They Thinking? 2. Discovering New Territories – Linux at Last! 3. Migrating to Swift 3 to Be More Swifty 4. Changes to Swifts Core Will Have You Asking for More 5. Function and Operator Changes – New Ways to Get Things Done 6. Extra, Extra Collection and Closure Changes That Rock! 7. Hold onto Your Chair; Advanced Type Changes Are Here! 8. Oh Goodness! Look Whats New in the Foundation Framework 9. Improving Your Code with Xcode Server and LLDB Debugging 10. Exploring Swift on the Server

Chapter 2. Discovering New Territories – Linux at Last!

Until recently, developing for Swift meant you needed a Mac, loaded with the Xcode IDE. However, all of that changed when Apple open sourced the Swift programming language in December 2015. A brave new world has been opened to us as developers, as Swift can now run on Linux! In addition, you now have access to preview releases and have direct access to the development trunk from which you can download development snapshots (for example, non-official prebuilt binaries of Swift).

This is going to be a packed chapter, and I want to highlight what we will cover. My goal is to show you where to find the latest Swift source for both Mac and Linux. I will also provide instruction on how to use toolchains and explain how the Swift package manager works. Last, we will develop our first program on Linux.

Downloading Swift


In order to get started working with Swift 3, you need to download either a prebuilt binary (also known as a toolchain) or the source code to build the Swift library yourself. The Swift.org (https://swift.org) website hosts a Download section https://swift.org/download/ that maintains a list of releases, previews and snapshots:

  • Release builds: Maintains links to the current release and older official releases of Swift.

  • Preview builds: Contains links to developer previews, also known as seeds or betas. These binaries are not considered final releases but do provide a fairly stable version of the work completed to that date for upcoming releases.

  • Developer snapshots - Are pre-built binaries from the development branch. These builds contain the latest development changes and have gone through automated unit testing but are not guaranteed to be stable. Snapshot builds are not put through the full testing process.

Note

Since learning to build the binary isn't critical to your...

Swift 3 on Mac


To get up and going on a Mac, you simply need to choose the type of Swift toolchain you want to develop against. You can choose a version from the Download section. Swift on a Mac is included with Xcode, making it really easy to get started. Swift 3 requires you to have macOS 10.11.5 (El Capitan) or later and Xcode 8. Let's walk through the steps together and install Swift 3 on a Mac.

  1. Download a toolchain - Grab the latest Swift 3 release or preview candidate from the downloads page on https://swift.org/. Xcode is created and maintained by Apple, selecting a release to download from https://swift.org/ will take you to Apple's downloads section on their developer portal.

    Note

    An Xcode toolchain is a special binary with a toolchain extension that includes Xcode and all of the tools and libraries that make up Swift (LLVM, LLDB, REPL, and other tools) all targeted at a specific Swift version. You can think of a toolchain as a bundled development environment that you install and...

Swift 3 on Linux


The Swift team currently supports installing Linux on Ubuntu 14.04 or 15.10 (64-bit). On Linux, Swift packages are distributed as tar archives. Each package includes the Swift compiler, the LLDB debugger, and tools related to doing development in Swift.

Note

If you don't have access to a Linux box, you can create a virtual machine using VirtualBox https://www.virtualbox.org and Vagrant https://www.vagrantup.com.

VirtualBox is a virtualization application that runs on multiple platforms and allows you to install another OS. You can download the latest version from https://www.virtualbox.org/wiki/Downloads.

Vagrant is a configuration and provisioning package that allows you to install and configure a complete development environment. You can find instructions on how to install and configure a Linux box at this location https://www.vagrantup.com/

  1. We need to install our required dependencies. Run the following command:

    $ sudo apt-get install clang libicu-dev
    
    • clang : The C language...

Using the REPL


Once we have Swift installed, we can use the Swift REPL (Read Evaluate Print Loop) environment and give Swift a test run on Linux. The Swift REPL environment and LLDB debugger are tightly linked to the toolchain, which aids in Swift type inference, syntax, and expression evaluation. Basically, it makes the compiler, debugger, and REPL environment's jobs easier if there is only one version of Swift to worry about at a time. Let's start the REPL environment and execute a few commands to get familiar with the REPL environment's capabilities.

To start the Swift REPL, you type the swift command:

$ swift

As we add statements, the REPL environment is smart enough to only execute once you have completely entered a statement. We can create assignment statements, functions, or even entire classes.

At the REPL prompt, let's assign:

1> let oneMillion = 1_000_000 
oneMillion: Int = 1000000 
2> let twoMillion: Int = 2_000_000 
twoMillion: Int = 2000000 
3> oneMillion...

Swift Package Manager


The Swift Package Manager is the Swift Army Knife that allows you to manage your code dependencies, share your own packages, and use the libraries created by others. It's an extremely important tool, one that you need to know in order to do anything productive with Swift. My goal is to provide you with a quick overview and then dive into some examples so we can use it in an example to solidify the core concepts.

Like other languages, Swift allows you to organize and group your Swift code. Swift refers to these groupings as modules. Modules in Swift allow the developer to enforce control on the functionality that is exposed publicly (outside of the module) and the functionality that is only visible within the module.

As developers, we use modules that we create or that other developers create to write our software. When we use other developers' modules, we create a dependency on their code. Swift allows us to create a package, which consists of the Swift code we write...

Our first Swift program


Let's create our first program on Linux using Swift. Our first project will be a package. Create a directory named guesswho and then enter the directory:

$ mkdir guesswho 
$ cd guess who

Next we need to initialize a new package with the type being an executable:

$ swift package init --type executable 
Creating executable package: guesswho 
Creating Package.swift 
Creating .gitignore 
Creating Sources/ 
Creating Sources/main.swift 
Creating Tests/

I want to point out a couple of things about the output of swift package init. First, using the swift package init command is optional and meant only to be a utility mechanism for generating files and directories you may need. Second, the package manager expects you to put your sources files within the Sources directory. You can further nest additional directories under the Sources directory and the package manager will treat those directories as modules. Finally, when you want to create...

Summary


In this chapter, we covered how to get your development environment configured for Swift development on a Mac or Linux machine. We learned about toolchains, using the REPL environment, and the Swift Package Manager. We also created our first Swift package, which we're able to execute on Linux. If you're still with me, we will cover even more awesome things in Swift in the forthcoming chapters! If you're observant, you've probably noticed that our example package lacked a few things. Rest assured, we will take a deep dive into creating and executing tests and debugging techniques in Chapter 9, Improving Your Code with Xcode Server and LLDB Debugging. We'll also come back to Linux to tackle a more complicated use case that includes adding dependencies to our package in Chapter 10, Exploring Swift on the Server.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Swift 3 New Features
Published in: Oct 2016 Publisher: Packt ISBN-13: 9781786469632
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 $15.99/month. Cancel anytime}