Swift fundamentals

Introduction

In this lesson, we’ll discuss the difference between constants and variables and go over some general rules for naming and declaring them. We also emphasize the importance of type inference in automatically determining the data type of a constant or variable.

Some general rules

No semicolons

It is recommended that each individual statement be placed on a separate line. In that case, semicolons (;) are not needed to indicate the end of the statement.

However, semicolons (;) are necessary if you want to write multiple statements on a single line.

Comment lines

// This is a single comment line

/* This long comment spans
two lines */

Comments can help make parts of the code easier to understand. A single comment line is denoted by //. Multiple comment lines must be enclosed between /* and */

Constants and Variables

Names

You can choose your own meaningful names for constants and variables. However, names cannot start with a digit and cannot contain special characters such as spaces, arrows, or math symbols. The Swift coding guidelines recommend using camelCase for naming conventions (the first letter of the name is lowercase, and the following words in the name always start with a capital letter).

The Swift language is case-sensitive. This means that it distinguishes between uppercase and lowercase letters. The variables lastName, LastName and lastname are three different variables in Swift.

Swift is a case-sensitive language!

Constants

A constant is assigned a value that cannot be changed. A constant must always be declared before it can be used. A constant can be defined with the let keyword:

constants

In the first line, you define a constant firstName of type String and assign the value “John” to it.

In the second line, you define a constant lastName and assign the value “Walters” to it. The constant lastName will be a String since you assign the String literal (“Walters”) to it.

Please note that it is not possible to change the values of constants. If you try to do so, you will receive an error message.

Variables

As the name suggests, the value assigned to a variable can still be changed. A variable must also be declared before it can be used. A variable can be declared with the var keyword:

Variables

In the first line, you define the variable totalInvoice of type Double and assign the value 4578.25.

In the second line, you define the variable productPrice and assign the value 45.67 to it. The variable productPrice will be of type Double , since the number 45.67 is considered a Double.

In most cases, you do not need to explicitly specify the type of a constant or variable in Swift. The ability to deduce the data type of another constant, variable, expression, etc.is called type inference.

Swift data types

The following table summarizes the main Swift data types:

Data typeDescription
IntInteger: used to store whole numbers.
DoubleUsed to store 64-bit decimal numbers.
FloatUsed to store 32-bit decimal numbers.
BoolBoolean: used to store a boolean value (true or false).
CharacterUsed to store a single character.
StringUsed to store a string of characters (text).
Swift data types
A Swift data type always starts with a capital letter!