Getting Started with Swift
Every programming language has some boilerplate material that we use to start a programming project. Mostly it is code to describe where the program should start and what additional libraries should be used to augment the programming language itself. Thus we have, in every language, this smallest program that we can write. We will ignore some of the explanation as to exactly what these minimal programs are doing until later. It is sufficient for now that we treat this as boilerplate code that forms the foundation of our programs. We can then expand upon these boilerplate programs as we explore the various features of the language.
#!/usr/bin/env swift; /****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation // Begin Main print("Hello, world!") exit(EXIT_SUCCESS)
This approach to introducing a programming language by writing a simple program that outputs one phrase, typically "Hello, world!" was first seen in Brian Kernighan and Dennis Ritchie's book "The C Programming Language" in the 1970s.
Language Execution Models
In order to get our programs as they are written in our favorite language to run on a computer, it is necessary to convert from the English-like text of our source code to something that the computer CPU can understand. Depending on the language, this either involves a compilation or interpretation phase. Some modern languages require both phases.
Compilation involves taking our source code and then translating it into another language, typically the language of the CPU known as machine language. Compilation is typically performed by a program called a compiler and it is a required step when writing in a compiled language. Once our program has been compiled into machine code, we can then run it directly on the CPU. Languages such as C, C++, Objective C, Swift, FORTRAN and Cobol are all compiled languages. These languages are typically used in the development of operating systems and commercially distributed code.
Because Swift uses the Compiled Language Execution Model we need to have the following software tools available to us: programming text editor to create/edit our source files, compiler and linker to translate our source files into a native executable program, and a command-line console to launch the program and handle the input and output from our program.
- In Linux the Command-Line Console is immediately available if booting in text mode. If booting in the Gnome graphical interface, it is typically available under the Application Menu -> Accessories -> Terminal.
- In Mac OS X the Command-Line Console is found in the Applications folder -> Utilities -> Terminal.
- On the Raspberry Pi the Command-Line Console is immediately available if booting in text mode. If booting in the Gnome graphical interface, it is available under the Application Menu -> Accessories -> Terminal.
- In Windows the Command-Line Console is found under the Start Menu -> All Programs -> Accessories -> Command Prompt. On Windows there are many more functional terminal emulator programs than the built-in one available such [[ConsoleZ]].
On this site we will use the terms console, terminal and command-line prompt interchangeably. Illustrations of terminal command sequences will be presented in a box with a black background. User input will appear in white while the command prompts ($) and program output will appear in green.
$ user input program output
Programming Text Editor
Programming Text Editors can be as simple as a plain text editor or as complicated as a text editor built-in to an Integrated Development Environment (IDE). For our purposes a middle-of-the-road programming editor will suffice. These types of text editors offer some niceties beyond the basic ability to edit text files. The most common of these programming oriented features is syntax highlighting which colors the parts of the programming text to indicate its role in the programming language's syntax. Other features include auto-completion, auto-balancing of brackets and auto-indenting of source code.
Programming Text Editors are not the same types of programs as Word Processing software. Word Processing software is designed to format and layout complex text documents such as manuals, brochures or flyers. The documents that they create are stored in special word processing formats that are not compatible with our programming tools. Programming Text Editors not only have features aimed at helping write programs, but more importantly they save the source files in a plain text format ([[ASCII]] or [[UTF-8]]) required by our programming tools.
While bare-bones text editors that come with your OS like TextEdit (Mac OS X), LeafEdit (Raspbian) or Notepad (Windows) will work to edit our source code files, you will be more productive using a programming text editor. Based on your operating system you might try some of the programming text editors listed below.
- Linux: [[GEdit]] (Free), [[Komodo Edit]] (Free), [[Sublime Text]] (Free), [[UltraEdit]], [[Visual Studio Code]] (Free)
- Mac OS X: [[GEdit]] (Free), [[BBEdit]], [[Komodo Edit]] (Free), [[Sublime Text]] (Free), [[TextMate 2]], [[UltraEdit]], [[Visual Studio Code]] (Free)
- Raspberry Pi: [[GEdit]] (Free)
- Windows: [[GEdit]] (Free), [[Komodo Edit]] (Free), [[Notepad++]] (Free), [[Sublime Text]] (Free), [[UltraEdit]], [[Visual Studio Code]] (Free)
Most Linux distributions based on the Gnome desktop will come with GEdit already installed. Some, however, like the Raspbian Wheezy distribution for the Raspberry Pi do not. If your distribution does not have GEdit installed it is easy to install from the command-line terminal using
$ sudo apt-get install gedit
Compiler and Linker
Currently Swift is only available on MacOS X. To install Swift, download XCode 11 or later from the Mac App store. To see if your system already has the Swift compiler tool chain installed, use the terminal to print the version of your installed compiler.
Swift went through some major changes during its development. This site will focus on version 5 only.
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]