Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
This book covers the development of iOS applications using the Swift programming language.
## Installing Swift
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.
### Installing on MacOS
Assuming you are running the latest version of MacOS (currently 10.13 High Sierra) you will be able to find [XCode 9 OR XCode 8.3.3 i
n the Labs with Sierra 10.12 pre-installed](https://itunes.apple.com/gb/app/xcode/id497799835?mt=12) 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.
### Installing on Ubuntu
You can download pre-compiled biaries from the [Swift Download Page](https://swift.org/download/). 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
```
## Code Editor
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](https://code.visualstudio.com) or [atom](https://atom.io) . This supports the Swift language and comes with an [integrated terminal](https://code.visualstudio.com/docs/editor/integrated-terminal) which we can use to run our scripts. For Atom editor, you may install a [Swift Atom package](https://atom.io/packages/language-swift). [A full starter's guide on how to launch and use Atom is here](http://flight-manual.atom.io/getting-started/sections/atom-basics/).
### Launching a code editor from the Terminal
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.
## Cloning the Lab Exercises
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](https://github.coventry.ac.uk/301CEM-1718SEPJAN/TEACHING-MATERIALS) 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_.
## Checking Version of Swift
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/
```
## Hello World
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.