Swift Documentation and Installing Swift
I spent a lot of my career working as a Linux System Administrator and Network Security Administrator. These positions required me to compile and install packages from source. There are a lot of advantages to building packages from source rather than downloading prebuilt binaries. The biggest advantage, in my opinion, is that you can get the latest version without having to wait for someone else to build it. This allows me to patch my systems with the latest security updates without delay. With Swift, we are also able to download the latest code and compile ourselves without having to wait for someone else to build it.
In this chapter, you will learn:
- About the swift.org website and what it offers
- How to find the latest documentation on Swift
- Ways to install Swift
- How to build Swift from source with its full toolchain and package manager
In the previous chapter, we mentioned that Apple has released...
On December 3, 2015, Apple officially released the Swift language, supporting libraries, the debugger, and the package manager to the open source community under the Apache 2.0 license. At that time, the swift.org site was created as the community's gateway to the project. This site has a wealth of information and should be your primary site to find out what is happening in the Swift community and the language itself. The blog posts will keep you up to date with new releases of Swift, new Swift open source libraries, changes to the standard library, and other Swift news.
You can also download pre-built binaries for several flavors of Linux. At the time this book is being written, we can download pre-built binaries for Ubuntu 16.04, Ubuntu 18.04, Ubuntu 20.04, CentOS 8, and Amazon Linux 2. The getting started page provides a list of dependencies for the previously mentioned flavors of Linux and instructions on how to install the binaries.
Apple and the Swift community, as a whole, have released a number of resources to help developers program in Swift. Apple's official documentation, which can be found at https://developer.apple.com/documentation/, includes API documentation for Swift as well as all of Apple's frameworks. Only a small portion of Apple's frameworks are open source and work across all platforms; however if you are looking to get started on one of Apple's frameworks this is definitely the place to start. However it can be hard to find documentation on specific Swift APIs.
To quickly find documentation on Swift's API, my favorite site is https://swiftdoc.org. This site is incredibly easy to navigate and has autogenerated documentation for all types, protocols, operators, and global functions that make up the Swift language. I have noticed that this site does not always stay up to date; however, it is a great reference for any Swift developer. The code that...
Installing Swift from swift.org
If you are developing on and for the Apple platform, I strongly suggest that you stick with the version of Swift that comes with Xcode. Apple will not approve an app that was compiled using a version of Swift that is different than the one that came with the version of Xcode that you are using. This may seem a little extreme, but it ensures that the apps are compiled with a stable version of Swift and one that has been fully vetted to work with your version of Xcode.
If you are using one of the flavors of Linux that has pre-built binaries on the swift.org site, it is recommended that you use those. They are the easiest and quickest path to getting Swift up and running. You can also find complete installation instructions with a list of dependencies on the swift.org site under the Getting Started section.
If pre-built binaries are not provided for your flavor of Linux, if you want to try the latest version of Swift, or if you just want...
Building Swift and the Swift toolchain from source
There are a number of sites that show you how to build Swift from source, but unfortunately most of these sites give directions for building just the Swift language itself without the toolchain. I find that not very useful, unless you are only writing very simple applications. In my opinion, building Swift for Linux without the entire toolchain and the package manager is more of an exercise of "Can I do it" rather than building something you can use long term.
While using the latest build of Swift is not recommended for production systems, it does enable us to use the latest features of the language and also verify that changes introduced in our application are compatible with future versions of the Swift language.
In this chapter, we will look at how we can build Swift, its entire toolchain, and the Swift package manager from source. Since each flavor of Linux and macOS are somewhat different, I need...
Using the Swift package manager
There is a lot that you can do with the package manager that makes it a necessity for creating complex applications on the Linux platform. It helps with adding dependencies to projects and enables us to break our code up into multiple files and create library projects. You can use the package manager on the Mac platform as well, but I do find it easier to use Xcode.
For the examples in this book, we will not need to add dependencies or use multiple files. Let's see how we can simply build and run an executable project in the package manager so you can use it to run the examples from this book if you would like. Keep in mind you are able to use the package manager on the Apple platform as well. When the package manager created
main.swift in the
Sources/test/ directory it added the following code to it:
This code gives us the basic
Hello World application. You can replace this code with examples from...
Using the Swift compiler
The Swift compiler is the basic utility to build Swift code and it is used by the package manager, Xcode, and any other utility that builds Swift code into executables. We can also call it ourselves. To see how to call it ourselves, create a file named
hello.swift and add the
print("Hello, world!") code to it as shown with the following code:
echo `print("Hello, world!")` >> hello.swift
Now we can compile this code with the following command, which calls the Swift compiler:
Finally we can execute the newly created application like we would any other executable:
And we will be greeted with our
Hello, world! message.
In this chapter, we looked at some of the different documentation that Apple and the Swift community offer. This documentation can be essential as you are learning Swift and also for reference once you have mastered the language itself. We also looked at how we can build and install Swift and its full toolchain. While using the latest build of Swift is not recommended for production systems, I usually keep a virtual machine or my desktop setup with a recent build. This enables me to use the latest features of the language and also run my code against it to make sure I am not introducing changes that are not compatible with future versions of the language.
In the next chapter, we will start to get into the language itself and we will see how to use variables and constants in Swift. We will also look at the various data types and how to use operators in Swift.