Swift Tutorial: Getting Started with the Basics

Swift TutorialSwift is a new and modern programming language designed to be easy to learn and fun to develop in. In this tutorial, we will go over the basics to get you started on your way to development with the Swift programming language.

Basic Syntax

The best place to start when learning a programming language would be the basic syntax. The syntax is very important and will be the foundation on which you build your programs and applications. The basic syntax uses a sequence of characters to form valid tokens of the language. Tokens consist of an identifier, keyword, punctuation, literal, or operator.

Identifier

In Swift, an identifier is a name used to identify any user-defined items. These can include items such as variables, functions, and classes. Identifiers use alphanumeric characters as well as the underscore character and must start with a letter or underscore. Special characters such as @, $, and % are not allowed to be used in identifiers.

Keywords

As with any programming language, there are a set of keywords that tells the compiler what to expect from your statement. These keywords are reserved and can not be used for any identifier such as variables, constants, function names, class names, etc unless you escape it with backticks (`). You will need to put a backtick before and after the identifier. For example, class is not a valid identifier, but `class` is valid.

Whitespace

In Swift, whitespace is a term to describe spaces, tabs, and newline characters. It is used to separate parts of a statement and to aid in readability. The compiler ignores a blank line containing only whitespace.

Comments

When writing your code, it is sometimes helpful to add comments to document the purpose of the code. This is especially important if other people work with your code. There are two different types of commenting styles you can use in Swift.

For comments that can fit on a single line, you can use the double slash (//). The compiler will ignore everything after the double slash on a line.

// This is a single line comment in Swift

var someVariable // This is also a single line comment to describe the variable

Sometimes you will need to write longer descriptions like at the beginning of the source file or might need to comment out a whole block of code. Multiline comments start with /* and terminate with */. The compiler will ignore anything in between those. You can use this style on a single line comment also, but usually, you would just use the double slash.

/* This will be comments for my Swift program.
 * It will include things like the author, date, and 
 * description.
 */

/* This is multi-line sytax but on a single line */

Semicolons

Programming languages like C, Java, C#, etc use semicolons to end each statement. In Swift the semicolons are optional. For consistency, you should use them throughout your program or omit them completely. The only exception to this is if you write multiple separate statements on a single line. The semicolon is required in that case.

// This is an example of putting two statements on a single line.
// This will declare a variable with the name of pet and assign it 
// the value "cat". It will then print the text "cat"
let pet = "cat"; print(pet)

Variables and Constants

Variables

A variable uses an identifier as a name to store values for a program to use. Since a variable is just an identifier the naming rules mentioned previously apply. In order to use a variable in a program, you must first declare it. To declare a variable use the var keyword.

var pet: String

Constants

Constants are variables that cannot change once assigned. They associate an identifier with a value of a particular type. Think of them as fixed values in the Swift program which will not alter during the execution of the program. Constants can be any of the basic data types like integers, float point numbers, characters, or strings.

Before using the constants, programmers must have to declare them using the keyword let.

let hello: String = "Hello World!"

Literals

Literals are a type of constant. They represent values within the source code of the program. In the previous example of a constant, “Hello World!” is a string literal. Some people might refer to this hard-coding because you use the actual value representation instead of a variable or input method.

Hello World

It wouldn’t be a programming tutorial without a “Hello World!” example. The following example will show how to print the string “Hello World!” to the screen using the print function in Swift.

/* This is an example program that will print out the text string
 * "Hello World!" to the standard output.
 */
print("Hello World!")

That is all there is to it. To see it in action you could copy the above example into an online Swift compiler and run it or you could copy it to a local file like HelloWorld.swift and compile it using the command line.

# swiftc HelloWorld.swift
# ./HelloWorld
Hello World!
#

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: