This book covers the development of iOS applications using the Swift programming language.
Whilst you will need a Mac running MacOS and with XCode installed to develop iOS apps, the Swift language itself is open source and can be installed on Linux and Windows computers.
Part 1 covers the Swift programming language and, because the Swift language is open source you will be able to complete the exercises using Ubuntu should you wish.
Assuming you are running the latest version of MacOS (currently 10.13 High Sierra) you will be able to find XCode 9 on the MacOS App Store. Once installed your Mac will handle updates. be aware the installation files come in at a whopping 4.5GB. Once you have installed XCode you will need to launch it so that you can agree the terms and conditions.
You can download pre-compiled biaries from the Swift Download Page. There are downloads for a number of different versions of Ubuntu and you can find binaries for a number of versions of Swift. You should be using the latest version of Swift 4 for the version of Ubuntu you are running. Start by downloading the latest version of Swift for you version of Ubuntu (in this case we are running 16.10) and substitute the correct file and folder names (HINT: use autocomplete).
sudo apt-get install clang libicu-dev
tar -xvzf ~/Downloads/swift-4.0-DEVELOPMENT-SNAPSHOT-2017-07-06-a-ubuntu16.10.tar.gz
sudo mv ~/Downloads/swift-4.0-DEVELOPMENT-SNAPSHOT-2017-07-06-a-ubuntu16.10 /usr/local
echo 'export PATH=/usr/local/swift-4.0-DEVELOPMENT-SNAPSHOT-2017-07-06-a-ubuntu16.10/usr/bin/:$PATH' >> ~/.profile
source ~/.profile
Whilst you should be using XCode for developing iOS apps, this is overkill for learning the language and it is recommended that you use a modern code editor such as Visual Studio Code. This supports the Swift language and comes with an integrated terminal which we can use to run our scripts.
You will be spending a lot of time in the terminal so here is a useful tip. If you have visual studio code installed you can launch it with the contents of a specified directory.
code ~/Documents/bookshop/
opens Visual Studio Code with the contents of the directory specified.code .
opens Visual Studio Code with the contents of the current directory.
If you are using Ubuntu this works 'out of the box' but if you are using MacOS you will need to edit your .bash_profile
file located in your home directory. Since this is hidden by default you can open it in nano using nano ~/.bash_profile
. Now add the following line, save the changes and exit.
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Close the terminal and re-launch. Now you can open Visual Studio Code using the same commands as Ubuntu.
This book adopts a hands-on approach to learning. Each concept will be introduced and then you will be shown a working script which you can use to see the concepts at work. Finally you will be practicing these new skills by making changes to the scripts in the Test Your Knowledge section.
All the lab exercises can be found on GitHub and your first task will be to clone the repository onto you local computer. Open the Terminal tool and clone the materials into your Documents/
directory.
$ git clone https://github.coventry.ac.uk/301CEM-1718SEPJAN/TEACHING-MATERIALS swift
This will create a new directory called swift/
.
Finally you can open this directory using Visual Studio Code.
Before we start its worth checking that we are using Swift 4 (the latest version at the time of writing).
$ swift -version
Apple Swift version 4.0 (swiftlang-802.0.53 clang-802.0.42)
Target: x86_64-apple-macosx10.9
Apple often release Beta versions of XCode which contain a more up to date version of the Swift language. If you want to use the version of Swift from the XCode Beta.
sudo xcode-select --switch /Applications/Xcode-beta.app/Contents/Developer/
By now you should have installed the Swift runtime, cloned the lab exercises and opened them in Visual Studio Code. Lets make sure your computer is set up properly, a task typically called hello world.
In Visual Studio Code open the Integrated Terminal either using the View menu or using ctrl+` (the _backtick character, not a single quote). This will open the integrated terminal in the current directory, you can check this using ls
command. You should see the directories and files in the swift/
directory.
We now need to navigate to the exercises/intro/
directory and run a script called hello.swift
. Note that the $
represents the prompt, you should enter the remainder of the line. A line that does not start with a $
represents the output of the script, in this case it should print Hello World
.
$ cd exercises/intro/
$ swift hello.swift
Hello World
If everything works as planned your computer is configured ready to start learning Swift.