Learning Swift - First Steps
Get started with an initial example application of Swift. Also, learn the use of semicolons and comments in Swift code.
We'll cover the following
In addition to a quick confidence boost for the novice, a simple initial example serves as a good opportunity to check that the environment used for the rest of this course is fully functional.
The first Swift example
When learning a programming language, it has become tradition to have the first example consist of code that displays “Hello World”. With this tradition in mind, we will begin with such an example.
In Swift, this can be achieved using the following line of code:
print("Hello World")
To see this in action, click the “Run” button below:
print("Hello World")
Congratulations, you have just run your first line of Swift code.
Before we start looking at Swift programming in more detail, we first need to take a few minutes to discuss the use of semicolons and code comments in Swift.
Swift and semicolons
Unlike programming languages such as Java, C, and C++, Swift does not require semicolons at the end of each statement or expression line. The following, therefore, is valid Swift code:
print("Welcome")
print("to Swift")
Semicolons are only required when multiple statements appear on the same line:
print("Welcome"); print("to Swift")
Commenting Swift code
Comments in both programming and scripting languages provide a mechanism for you to write notes that the compiler or interpreter ignores. These notes are intended solely for you and anyone else who may later need to modify the code. The main purpose of comments is to allow you to make notes that help anyone who may read the code later. These notes help readers understand issues, such as how a particular section of a program works, what a particular method does, or what a variable is used to store. Commenting code is considered to be good practice. Rest assured that a section of Swift code that seems obvious when you write it will often be confusing when you return to it months or even years later to modify it. By including explanatory comments alongside the code, this becomes less of a problem.
Single-line comments
A single-line comment is marked by prefixing the line with //
(this will be familiar to C++, Kotlin, or Java programmers). For example:
// This is a comment line. It is for human use only and is ignored by the Swift compiler.
var pet: String = "Cat"
print(pet)
// This is another comment
The //
syntax tells the compiler that everything on the same line after the // is a comment and should be ignored. This means that anything on the line before the //
comment marker is not ignored. The advantage of this is it enables comments to be placed at the end of a line of code. For example:
var myString = "Welcome to Swift" // Variable containing pointer to welcome string object
In the above example, everything after the //
marker is considered a comment. Therefore, it is ignored by the Swift compiler. This provides an ideal method for placing comments on the same line of code that describe what that particular line of code does.
Multi-line comments
For the purposes of supporting comments that extend over multiple lines, Swift borrowed some syntax from the C programming language. The start and end of lines of comments are marked by the /*
and */
markers respectively. Everything immediately after the /*
and before the */
is considered to be a comment, regardless of where the markers appear on a line. For example:
/*
This function adds two numbers together
and returns the result of the addition
*/
func addNumbers(val1: Int, val2: Int) -> Int {
return val1 + val2
}
Multi-line comments are particularly useful for commenting out sections of a program that you no longer wish to run but do not yet want to delete. It’s best practice to leave a note of when and why you have commented out a section so later readers can understand your thought process.
/*
Commented out December 23 while testing an improved version
var myValue = 10
let myString = "My lucky number is "
print("\(myString) \(myValue)")
*/
In the above example, everything between the /*
and */
markers is considered to be a comment. Even though this content contains valid Swift code, it is ignored by the compiler.
Lesson recap
- You have just executed your first line of Swift code.
- Semi-colons at the end of statements are optional in Swift.
- Code will be easier to understand for you and other programmers if it is commented.
- Swift provides single (
//
) and multi-line (/* */
) comment markers.
Quiz
Take this quiz to test your knowledge of Swift comments and the use of semicolons.
Select the true statement:
Swift does not require a semi-colon at the end of each statement or expression line.
Swift requires a semicolon at the end of each statement or expression line.